本文整理汇总了Java中org.apache.mina.transport.socket.SocketConnector类的典型用法代码示例。如果您正苦于以下问题:Java SocketConnector类的具体用法?Java SocketConnector怎么用?Java SocketConnector使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SocketConnector类属于org.apache.mina.transport.socket包,在下文中一共展示了SocketConnector类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setConnector
import org.apache.mina.transport.socket.SocketConnector; //导入依赖的package包/类
/**
* Sets the {@link SocketConnector} to be used for connections
* to the proxy server.
*
* @param connector the connector to use
*/
private final void setConnector(final SocketConnector connector) {
if (connector == null) {
throw new IllegalArgumentException("connector cannot be null");
}
this.connector = connector;
String className = ProxyFilter.class.getName();
// Removes an old ProxyFilter instance from the chain
if (connector.getFilterChain().contains(className)) {
connector.getFilterChain().remove(className);
}
// Insert the ProxyFilter as the first filter in the filter chain builder
connector.getFilterChain().addFirst(className, proxyFilter);
}
示例2: connect
import org.apache.mina.transport.socket.SocketConnector; //导入依赖的package包/类
/**
* <p>Connect to a remote socket. If org.opennms.netmgt.provision.maxConcurrentConnections
* is set, this may block until a connection slot is available.</p>
*
* <p>You must dispose both the {@link ConnectionFactoryNewConnectorImpl} and {@link ConnectFuture} when done
* by calling {@link #dispose(ConnectionFactoryNewConnectorImpl, ConnectFuture)}.</p>
*
* @param remoteAddress
* Destination address
* @param init
* Initialiser for the IoSession
* @return
* ConnectFuture from a Mina connect call
*/
@Override
public ConnectFuture connect(SocketAddress remoteAddress, IoSessionInitializer<? extends ConnectFuture> init, IoHandler handler) {
SocketConnector connector = getSocketConnector(getTimeout(), handler);
InetSocketAddress localAddress = null;
synchronized (m_portMutex) {
if (m_port.get() == null) {
// Fetch a new ephemeral port
localAddress = new InetSocketAddress(0);
m_port.set(localAddress.getPort());
} else {
localAddress = new InetSocketAddress(m_port.get());
}
}
final ConnectFuture cf = connector.connect(remoteAddress, localAddress, init);
cf.addListener(portSwitcher(connector, remoteAddress, init, handler));
cf.addListener(connectorDisposer(connector));
return cf;
}
示例3: testProducerShutdownTestingWithMock
import org.apache.mina.transport.socket.SocketConnector; //导入依赖的package包/类
@Test
public void testProducerShutdownTestingWithMock() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedBodiesReceived("Hello World");
// create our mock and record expected behavior = that worker timeout should be set to 0
SocketConnector mockConnector = createMock(SocketConnector.class);
mockConnector.dispose(true);
replay(mockConnector);
// normal camel code to get a producer
Endpoint endpoint = context.getEndpoint(String.format("mina2:tcp://localhost:%1$s?textline=true&sync=false", getPort()));
Exchange exchange = endpoint.createExchange();
Producer producer = endpoint.createProducer();
producer.start();
// set input and execute it
exchange.getIn().setBody("Hello World");
producer.process(exchange);
// insert our mock instead of real MINA IoConnector
Field field = producer.getClass().getDeclaredField("connector");
field.setAccessible(true);
field.set(producer, mockConnector);
//
// Everything is asynchronous.
// We need to wait a second to make sure we get the message.
//
Thread.sleep(1000);
// stop using our mock
producer.stop();
verify(mockConnector);
assertMockEndpointsSatisfied();
}
示例4: 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();
}
}
示例5: 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);
}
}
}
示例6: ProxyConnector
import org.apache.mina.transport.socket.SocketConnector; //导入依赖的package包/类
/**
* Creates a new proxy connector.
* @see AbstractIoConnector(IoSessionConfig, Executor).
*/
public ProxyConnector(final SocketConnector connector, IoSessionConfig config, Executor executor) {
super(config, executor);
setConnector(connector);
}
示例7: 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));
}
示例8: getConnector
import org.apache.mina.transport.socket.SocketConnector; //导入依赖的package包/类
/**
* Get the {@link SocketConnector} to be used for connections
* to the proxy server.
*/
public final SocketConnector getConnector() {
return connector;
}
示例9: setupConnector
import org.apache.mina.transport.socket.SocketConnector; //导入依赖的package包/类
protected abstract void setupConnector ( SocketConnector connector );