use of org.apache.mina.transport.socket.nio.SocketConnector in project camel by apache.
the class MinaComponent method createSocketEndpoint.
protected MinaEndpoint createSocketEndpoint(String uri, MinaConfiguration configuration) {
boolean minaLogger = configuration.isMinaLogger();
long timeout = configuration.getTimeout();
boolean sync = configuration.isSync();
List<IoFilter> filters = configuration.getFilters();
final int processorCount = Runtime.getRuntime().availableProcessors() + 1;
ExecutorService acceptorPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaSocketAcceptor");
ExecutorService connectorPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaSocketConnector");
ExecutorService workerPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaThreadPool");
IoAcceptor acceptor = new SocketAcceptor(processorCount, acceptorPool);
IoConnector connector = new SocketConnector(processorCount, connectorPool);
SocketAddress address = new InetSocketAddress(configuration.getHost(), configuration.getPort());
// connector config
SocketConnectorConfig connectorConfig = new SocketConnectorConfig();
// must use manual thread model according to Mina documentation
connectorConfig.setThreadModel(ThreadModel.MANUAL);
configureCodecFactory("MinaProducer", connectorConfig, configuration);
connectorConfig.getFilterChain().addLast("threadPool", new ExecutorFilter(workerPool));
if (minaLogger) {
connectorConfig.getFilterChain().addLast("logger", new LoggingFilter());
}
appendIoFiltersToChain(filters, connectorConfig.getFilterChain());
// set connect timeout to mina in seconds
connectorConfig.setConnectTimeout((int) (timeout / 1000));
// acceptor connectorConfig
SocketAcceptorConfig acceptorConfig = new SocketAcceptorConfig();
// must use manual thread model according to Mina documentation
acceptorConfig.setThreadModel(ThreadModel.MANUAL);
configureCodecFactory("MinaConsumer", acceptorConfig, configuration);
acceptorConfig.setReuseAddress(true);
acceptorConfig.setDisconnectOnUnbind(true);
acceptorConfig.getFilterChain().addLast("threadPool", new ExecutorFilter(workerPool));
if (minaLogger) {
acceptorConfig.getFilterChain().addLast("logger", new LoggingFilter());
}
appendIoFiltersToChain(filters, acceptorConfig.getFilterChain());
MinaEndpoint endpoint = new MinaEndpoint(uri, this);
endpoint.setAddress(address);
endpoint.setAcceptor(acceptor);
endpoint.setAcceptorConfig(acceptorConfig);
endpoint.setConnector(connector);
endpoint.setConnectorConfig(connectorConfig);
endpoint.setConfiguration(configuration);
// enlist threads pools in use on endpoint
endpoint.addThreadPool(acceptorPool);
endpoint.addThreadPool(connectorPool);
endpoint.addThreadPool(workerPool);
// set sync or async mode after endpoint is created
if (sync) {
endpoint.setExchangePattern(ExchangePattern.InOut);
} else {
endpoint.setExchangePattern(ExchangePattern.InOnly);
}
return endpoint;
}
use of org.apache.mina.transport.socket.nio.SocketConnector in project camel by apache.
the class MinaProducerShutdownMockTest method testProducerShutdownTestingWithMock.
@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.setWorkerTimeout(0);
replay(mockConnector);
// normal camel code to get a producer
Endpoint endpoint = context.getEndpoint("mina:tcp://localhost:{{port}}?textline=true&sync=false");
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);
// stop using our mock
producer.stop();
verify(mockConnector);
assertMockEndpointsSatisfied();
}
use of org.apache.mina.transport.socket.nio.SocketConnector in project dubbo by alibaba.
the class MinaClient method doOpen.
@Override
protected void doOpen() throws Throwable {
connectorKey = getUrl().toFullString();
SocketConnector c = connectors.get(connectorKey);
if (c != null) {
connector = c;
} else {
// set thread pool.
connector = new SocketConnector(Constants.DEFAULT_IO_THREADS, Executors.newCachedThreadPool(new NamedThreadFactory("MinaClientWorker", true)));
// config
SocketConnectorConfig cfg = (SocketConnectorConfig) connector.getDefaultConfig();
cfg.setThreadModel(ThreadModel.MANUAL);
cfg.getSessionConfig().setTcpNoDelay(true);
cfg.getSessionConfig().setKeepAlive(true);
int timeout = getTimeout();
cfg.setConnectTimeout(timeout < 1000 ? 1 : timeout / 1000);
// set codec.
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this)));
connectors.put(connectorKey, connector);
}
}
use of org.apache.mina.transport.socket.nio.SocketConnector in project dubbo by alibaba.
the class MinaClient method doOpen.
@Override
protected void doOpen() throws Throwable {
connectorKey = getUrl().toFullString();
SocketConnector c = CONNECTORS.get(connectorKey);
if (c != null) {
connector = c;
} else {
// set thread pool.
connector = new SocketConnector(Constants.DEFAULT_IO_THREADS, Executors.newCachedThreadPool(new NamedThreadFactory("MinaClientWorker", true)));
// config
SocketConnectorConfig cfg = (SocketConnectorConfig) connector.getDefaultConfig();
cfg.setThreadModel(ThreadModel.MANUAL);
cfg.getSessionConfig().setTcpNoDelay(true);
cfg.getSessionConfig().setKeepAlive(true);
int timeout = getConnectTimeout();
cfg.setConnectTimeout(timeout < 1000 ? 1 : timeout / 1000);
// set codec.
connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this)));
CONNECTORS.put(connectorKey, connector);
}
}
Aggregations