本文整理汇总了Java中org.apache.mina.transport.socket.SocketConnector.setHandler方法的典型用法代码示例。如果您正苦于以下问题:Java SocketConnector.setHandler方法的具体用法?Java SocketConnector.setHandler怎么用?Java SocketConnector.setHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.mina.transport.socket.SocketConnector
的用法示例。
在下文中一共展示了SocketConnector.setHandler方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testClassLoaderLeak2
import org.apache.mina.transport.socket.SocketConnector; //导入方法依赖的package包/类
@Test
public void testClassLoaderLeak2() throws Exception {
int max = 10000;
File sourceFile = new File(reloadSourceDir);
URL[] urls = new URL[]{sourceFile.toURL()};
// ReloadProtocolCodecFilter filter = ReloadProtocolCodecFilter.getInstance(
// GameServer.PROTOCOL_CODEC, GameServer.PROTOCOL_HANDLER, urls);
SocketConnector connector = new NioSocketConnector();
// connector.getFilterChain().addLast("codec", filter);
connector.setHandler(new ClientHandler());
//Send 1000 connections.
try {
for ( int i=0; i<Integer.MAX_VALUE; i++ ) {
ConnectFuture future = connector.connect(new InetSocketAddress("localhost", 3443));
future.awaitUninterruptibly();
IoSession session = future.getSession();
IoBuffer buffer = IoBuffer.allocate(8);
buffer.putShort((short)8);
buffer.putShort((short)0);
buffer.putInt(i);
WriteFuture wfuture = session.write(buffer);
wfuture.awaitUninterruptibly();
}
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
示例2: main
import org.apache.mina.transport.socket.SocketConnector; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable
{
IoBuffer.setUseDirectBuffer(false);
IoBuffer.setAllocator(new SimpleBufferAllocator());
SocketConnector connector =
new NioSocketConnector(Runtime.getRuntime().availableProcessors() + 1);
// Configure the service.
connector.setConnectTimeoutMillis(CONNECT_TIMEOUT);
connector.getFilterChain().addLast("threadPool", new ExecutorFilter(Executors.newFixedThreadPool(MAX_THREADS)));
connector.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
connector.getFilterChain().addLast("logger", new LoggingFilter());
SMTPSessionHandler handler = new SMTPSessionHandler("localhost");
connector.setHandler(handler);
while (true)
{
try
{
for (int i = 0; i < 10; i++)
connector.connect(new InetSocketAddress(HOSTNAME, PORT));
Thread.sleep(100);
}
catch (RuntimeIoException e)
{
System.err.println("Failed to connect.");
e.printStackTrace();
Thread.sleep(1000);
}
}
}
示例3: main
import org.apache.mina.transport.socket.SocketConnector; //导入方法依赖的package包/类
public static void main(String[] args) throws InvalidProtocolBufferException {
SocketConnector connector = new NioSocketConnector();
connector.setConnectTimeoutMillis(30 * 1000);
DefaultProtocolCodecFactory defaultProtocolCodecFactory=new DefaultProtocolCodecFactory();
defaultProtocolCodecFactory.setDecoder(new ProtocolBufferDecoder());
defaultProtocolCodecFactory.setEncoder(new ProtocolBufferEncoder());
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(defaultProtocolCodecFactory));
connector.setHandler(new Client());
ConnectFuture future = connector.connect((SocketAddress)SpringContext.getBean("gameServerBindAddress"));
future.awaitUninterruptibly();
IoSession session=future.getSession();
Person persion=Test.Person.newBuilder().setId(1).setName("侧方dfdfdfd").build();
// IResponseMessage msg=new ResponseMessage(1, 1,persion);
// client.write(msg);
/**
* <body>
* <pre>
* BEFORE ENCODE AFTER ENCODE
* +-----------+-------------+------------+ +----------+
* | actionID | datalength | Data | -----> | Data |
* | short | short | bytes(300) | | (bytes)|
* +-----------+-------------+------------+ +----------+
* </pre>
* </body>
*/
short ioSize = (short) persion.toByteArray().length;
IoBuffer buffer = IoBuffer.allocate(ioSize + 8).setAutoExpand(true);
buffer.putShort((short)1);
buffer.putShort(ioSize);
buffer.put( persion.toByteArray());
buffer.flip();
// session.write(buffer);
persion=Test.Person.newBuilder().mergeFrom( persion.toByteArray()).build();
System.out.println( TextFormat.printToUnicodeString(persion));
}