本文整理汇总了Java中org.jboss.netty.bootstrap.ClientBootstrap.connect方法的典型用法代码示例。如果您正苦于以下问题:Java ClientBootstrap.connect方法的具体用法?Java ClientBootstrap.connect怎么用?Java ClientBootstrap.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jboss.netty.bootstrap.ClientBootstrap
的用法示例。
在下文中一共展示了ClientBootstrap.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
public void start(String ip, int port) {
// Configure the client.
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("java.net.preferIPv6Addresses", "false");
bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// Set up the event pipeline factory.
// bootstrap.setPipelineFactory(new MessageServerPipelineFactory());
bootstrap.getPipeline().addLast("decoder", new PackageDecoder());
bootstrap.getPipeline().addLast("encoder", new PackageEncoder());
bootstrap.getPipeline().addLast("handler", new ClientHandler());
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
bootstrap.setOption("child.linger", 1);
// Start the connection attempt.
channelFuture = bootstrap.connect(new InetSocketAddress(ip, port));
// Wait until the connection is closed or the connection attempt fails.
channelFuture.awaitUninterruptibly();
channel = channelFuture.awaitUninterruptibly().getChannel();
}
示例2: run
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
public void run() {
// Configure the client.
ChannelFactory factory = new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 1, 1);
ClientBootstrap bootstrap = new ClientBootstrap(factory);
// Set up the pipeline factory.
bootstrap.setPipelineFactory(setPipelineFactory());
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
// Start the connection attempt.
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
if (oneShot) {
// Wait until the connection is closed or the connection attempt fails.
future.getChannel().getCloseFuture().awaitUninterruptibly();
// Shut down thread pools to exit.
bootstrap.releaseExternalResources();
}
}
示例3: connect
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
/**
* Starts the BGP peer.
*
* @param connectToSocket the socket to connect to
*/
private void connect(InetSocketAddress connectToSocket)
throws InterruptedException {
ChannelFactory channelFactory =
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
ChannelPipelineFactory pipelineFactory = () -> {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("BgpPeerFrameDecoderTest",
peerFrameDecoder);
pipeline.addLast("BgpPeerChannelHandlerTest",
peerChannelHandler);
return pipeline;
};
peerBootstrap = new ClientBootstrap(channelFactory);
peerBootstrap.setOption("child.keepAlive", true);
peerBootstrap.setOption("child.tcpNoDelay", true);
peerBootstrap.setPipelineFactory(pipelineFactory);
peerBootstrap.connect(connectToSocket);
}
示例4: addNewSwitch
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
private void addNewSwitch(DummySwitch dummySwitch) {
final SwitchChannelHandler switchHandler = new SwitchChannelHandler(coreConnector, aggreedVersion, moduleName);
switchHandler.setDummySwitch(dummySwitch); // CONTAINS ALL THE INFO
// ABOUT THIS SWITCH
ChannelFactory factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
ClientBootstrap bootstrap = new ClientBootstrap(factory);
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("keepAlive", true);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() {
return Channels.pipeline(switchHandler);
}
});
// CONNECT AND ADD TO HASHMAP OF MANAGED SWITCHES
ChannelFuture future = bootstrap.connect(new InetSocketAddress("localhost", 7753));
managedSwitchesChannel.put(dummySwitch.getDatapathId(), future);
managedBootstraps.put(dummySwitch.getDatapathId(), bootstrap);
managedSwitches.put(dummySwitch.getDatapathId(), dummySwitch);
switchHandler.registerSwitchConnection(future);
switchHandler.setModuleHandler(moduleHandler);
}
示例5: prepare
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
public void prepare(Map stormConf, TopologyContext context,
final OutputCollector collector) {
_collector = collector;
ChannelFactory factory = new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
_bootstrap = new ClientBootstrap(factory);
_bootstrap.setPipelineFactory(getPipelineFactory());
_bootstrap.setOptions(_options);
ChannelFuture future = _bootstrap.connect(new InetSocketAddress(_host, _port));
int connectTimeout = DEFAULT_CONNECT_TIMEOUT;
Object connectTimeoutConfig = stormConf.get(Config.NIMBUS_TASK_LAUNCH_SECS);
if (connectTimeoutConfig != null) {
connectTimeout = ((Number)connectTimeoutConfig).intValue()*1000/2;
}
future.awaitUninterruptibly(connectTimeout);
if (!future.isSuccess()) {
_bootstrap.releaseExternalResources();
throw new RuntimeException("Could not connect to '"+_host+":"+_port, future.getCause());
}
_channel = future.getChannel();
}
示例6: channelOpen
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) {
final Channel inboundChannel = e.getChannel();
RtmpProxy.ALL_CHANNELS.add(inboundChannel);
inboundChannel.setReadable(false);
ClientBootstrap cb = new ClientBootstrap(cf);
cb.getPipeline().addLast("handshaker", new ProxyHandshakeHandler());
cb.getPipeline().addLast("handler", new OutboundHandler(e.getChannel()));
ChannelFuture f = cb.connect(new InetSocketAddress(remoteHost, remotePort));
outboundChannel = f.getChannel();
f.addListener(new ChannelFutureListener() {
@Override public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
logger.info("connected to remote host: {}, port: {}", remoteHost, remotePort);
inboundChannel.setReadable(true);
} else {
inboundChannel.close();
}
}
});
}
示例7: testChannelConnectAndDisconnect
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
@Test
public void testChannelConnectAndDisconnect() throws Exception {
IsdnClientChannelFactory isdnClientFactory = new IsdnClientChannelFactory(Executors.newCachedThreadPool(),
capiFactory, getConfigurator());
ClientBootstrap client = new ClientBootstrap(isdnClientFactory);
// CONNECT
long c0 = System.currentTimeMillis();
ChannelFuture channelFuture = client.connect(calledAddress, callingAddress);
channelFuture.await(AWAIT_TIMEOUT);
long c1 = System.currentTimeMillis();
LOGGER.debug("testChannelConnectAndDisconnect() :: Connect performed in {}ms", (c1 - c0));
// few delay to simulate a network application activity
// Thread.sleep(10);
// DISCONNECT
long d0 = System.currentTimeMillis();
ChannelFuture closeFuture = channelFuture.getChannel().close();
closeFuture.await(AWAIT_TIMEOUT);
long d1 = System.currentTimeMillis();
LOGGER.debug("testChannelConnectAndDisconnect() :: Disconnect performed in {}ms", (d1 - d0));
}
示例8: createChannel
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
/**
* Creates a new channel to given host and port.<br>
*
* @param host
* @param port
* @return
* @throws Exception
*/
private Channel createChannel(String host, int port) throws Exception {
// Important notice; use NioClientSocketChannelFactory instead
// of NioServerSocketChannelFactory
ChannelFactory channelFactory = new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
ClientBootstrap bootstrap = new ClientBootstrap(channelFactory);
//bootstrap.setPipelineFactory(new SipClientPipelineFactory(false,false));
bootstrap.setPipelineFactory(new SipPipelineFactory(sipServerHandler));
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
// open / connect to channel
Channel c = future.await().getChannel();
if (!future.isSuccess()) {
log.warn(String.format("createChannel. Establishing connection failed[%s]",
future.getCause().getMessage()));
bootstrap.releaseExternalResources();
}
return c;
}
示例9: channelOpen
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
logger.info("->");
// Suspend incoming traffic until connected to the remote host.
final Channel inboundChannel = e.getChannel();
inboundChannel.setReadable(false);
// Start the connection attempt.
ClientBootstrap cb = new ClientBootstrap(cf);
cb.getPipeline().addLast("handler", new OutboundHandler(e.getChannel()));
ChannelFuture f = cb.connect(new InetSocketAddress(remoteHost, remotePort));
outboundChannel = f.getChannel();
f.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
// Connection attempt succeeded:
// Begin to accept incoming traffic.
inboundChannel.setReadable(true);
} else {
// Close the connection if the connection attempt has failed.
inboundChannel.close();
}
}
});
logger.info("<-");
}
示例10: channelOpen
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
@Override
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
// Suspend incoming traffic until connected to the remote host.
final Channel inboundChannel = e.getChannel();
inboundChannel.setReadable(false);
// Start the connection attempt.
ClientBootstrap cb = new ClientBootstrap(cf);
cb.getPipeline().addLast("handler", new OutboundHandler(e.getChannel()));
ChannelFuture f = cb.connect(new InetSocketAddress(remoteHost, remotePort));
outboundChannel = f.getChannel();
f.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
// Connection attempt succeeded:
// Begin to accept incoming traffic.
inboundChannel.setReadable(true);
} else {
// Close the connection if the connection attempt has failed.
inboundChannel.close();
}
}
});
}
示例11: main
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
public static void main(String args[]) {
// Client服务启动器
ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
// 设置一个处理服务端消息和各种消息事件的类(Handler)
bootstrap.setPipelineFactory(() -> Channels.pipeline(new HelloClientHandler()));
// 连接到本地的8000端口的服务端
bootstrap.connect(new InetSocketAddress("127.0.0.1", 8000));
}
示例12: createClient
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
protected Client createClient(String targetIP, int targetPort, int connectTimeout, String key) throws Exception {
ClientBootstrap bootstrap = new ClientBootstrap(nioClient);
bootstrap.setOption("tcpNoDelay", Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true")));
bootstrap.setOption("reuseAddress", Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true")));
if (connectTimeout < 1000) {
bootstrap.setOption("connectTimeoutMillis", 1000);
} else {
bootstrap.setOption("connectTimeoutMillis", connectTimeout);
}
NettyClientHandler handler = new NettyClientHandler(this, key);
bootstrap.setPipelineFactory(new NettyClientPipelineFactory(handler));
ChannelFuture future = bootstrap.connect(new InetSocketAddress(targetIP, targetPort));
future.awaitUninterruptibly(connectTimeout);
if (!future.isDone()) {
LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " timeout!");
throw new Exception("Create connection to " + targetIP + ":" + targetPort + " timeout!");
}
if (future.isCancelled()) {
LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!");
throw new Exception("Create connection to " + targetIP + ":" + targetPort + " cancelled by user!");
}
if (!future.isSuccess()) {
LOGGER.error("Create connection to " + targetIP + ":" + targetPort + " error", future.getCause());
throw new Exception("Create connection to " + targetIP + ":" + targetPort + " error", future.getCause());
}
NettyClient client = new NettyClient(future, key, connectTimeout);
handler.setClient(client);
return client;
}
示例13: testNettyChannelWriting
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
@Test(timeOut = 10_000)
public void testNettyChannelWriting() throws Exception {
// ------------------------------------------------------------------------------------------------------------
// Prepare test
// ------------------------------------------------------------------------------------------------------------
// Connect channel handler
channelHandler.reconnect();
// Create client and connect it
ClientBootstrap nettyClient = createNettyClientBootstrap();
ChannelFuture channelF = nettyClient.connect(new InetSocketAddress("localhost", 1434));
// Basic checks for connection
while (!channelF.isDone()) /** do nothing */ ;
assertTrue(channelF.isSuccess());
assertTrue(channelF.getChannel().isConnected());
Channel channel = channelF.getChannel();
// Eventually the channel group of the server should have 2 elements
while (channelHandler.channelGroup.size() != 2) /** do nothing */ ;
// Write first handshake request
TSOProto.HandshakeRequest.Builder handshake = TSOProto.HandshakeRequest.newBuilder();
// NOTE: Add here the required handshake capabilities when necessary
handshake.setClientCapabilities(TSOProto.Capabilities.newBuilder().build());
channelF.getChannel().write(TSOProto.Request.newBuilder().setHandshakeRequest(handshake.build()).build());
// ------------------------------------------------------------------------------------------------------------
// Test channel writing
// ------------------------------------------------------------------------------------------------------------
testWritingTimestampRequest(channel);
testWritingCommitRequest(channel);
testWritingFenceRequest(channel);
}
示例14: startClient
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
public void startClient() {
ClientBootstrap bootstrap = new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
try {
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
ChannelPipeline p = Channels.pipeline();
handler = new NettyClientHandler();
p.addLast("handler", handler);
return p;
}
});
bootstrap.setOption("tcpNoDelay", true);
bootstrap.setOption("receiveBufferSize", 1048576);
bootstrap.setOption("sendBufferSize", 1048576);
// Start the connection attempt.
LOG.info("EventClient: Connecting " + host + "," + port);
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
LOG.info("EventClient: Connected " + host + "," + port);
allChannels = new DefaultChannelGroup();
// Wait until the connection is closed or the connection attempt fails.
allChannels.add(future.getChannel());
LOG.info("EventClient: Added to Channels ");
} catch (Exception e) {
e.printStackTrace();
}
}
示例15: channelOpen
import org.jboss.netty.bootstrap.ClientBootstrap; //导入方法依赖的package包/类
@Override
public void channelOpen(ChannelHandlerContext context, ChannelStateEvent event) throws Exception {
// Suspend incoming traffic until connected to the remote host.
final Channel sourceChannel = event.getChannel();
sourceChannel.setReadable(false);
boolean isBlocked = blocked.get();
LOG.debug("Attempt to open proxy channel from [{}] to [{}:{}] in state [blocked = {}]",
sourceChannel.getLocalAddress(),
remoteHost,
remotePort,
isBlocked);
if (isBlocked) {
sourceChannel.close();
return;
}
// Start the connection attempt.
ClientBootstrap targetConnectionBootstrap = new ClientBootstrap(channelFactory);
targetConnectionBootstrap.getPipeline().addLast(TARGET_CHANNEL_HANDLER_NAME, new TargetChannelHandler(event.getChannel(), blocked));
ChannelFuture connectFuture = targetConnectionBootstrap.connect(new InetSocketAddress(remoteHost, remotePort));
sourceToTargetChannels.put(sourceChannel, connectFuture.getChannel());
connectFuture.addListener(future -> {
if (future.isSuccess()) {
// Connection attempt succeeded:
// Begin to accept incoming traffic.
sourceChannel.setReadable(true);
} else {
// Close the connection if the connection attempt has failed.
sourceChannel.close();
}
});
}