use of java.net.InetSocketAddress in project camel by apache.
the class MinaComponent method createDatagramEndpoint.
protected MinaEndpoint createDatagramEndpoint(String uri, MinaConfiguration configuration) {
boolean minaLogger = configuration.isMinaLogger();
long timeout = configuration.getTimeout();
boolean transferExchange = configuration.isTransferExchange();
boolean sync = configuration.isSync();
List<IoFilter> filters = configuration.getFilters();
ExecutorService acceptorPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaDatagramAcceptor");
ExecutorService connectorPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaDatagramConnector");
ExecutorService workerPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaThreadPool");
IoAcceptor acceptor = new DatagramAcceptor(acceptorPool);
IoConnector connector = new DatagramConnector(connectorPool);
SocketAddress address = new InetSocketAddress(configuration.getHost(), configuration.getPort());
if (transferExchange) {
throw new IllegalArgumentException("transferExchange=true is not supported for datagram protocol");
}
DatagramConnectorConfig connectorConfig = new DatagramConnectorConfig();
// must use manual thread model according to Mina documentation
connectorConfig.setThreadModel(ThreadModel.MANUAL);
configureDataGramCodecFactory("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));
DatagramAcceptorConfig acceptorConfig = new DatagramAcceptorConfig();
// must use manual thread model according to Mina documentation
acceptorConfig.setThreadModel(ThreadModel.MANUAL);
configureDataGramCodecFactory("MinaConsumer", acceptorConfig, configuration);
acceptorConfig.setDisconnectOnUnbind(true);
// reuse address is default true for datagram
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 java.net.InetSocketAddress in project camel by apache.
the class MinaTcpLineDelimiterUsingPlainSocketTest method sendAndReceive.
private String sendAndReceive(String input) throws IOException {
byte[] buf = new byte[128];
Socket soc = new Socket();
soc.connect(new InetSocketAddress("localhost", getPort()));
// Send message using plain Socket to test if this works
OutputStream os = null;
InputStream is = null;
try {
os = soc.getOutputStream();
// must append MAC newline at the end to flag end of textline to Camel-Mina
os.write((input + "\r").getBytes());
is = soc.getInputStream();
int len = is.read(buf);
if (len == -1) {
// no data received
return null;
}
} finally {
IOHelper.close(is, os);
soc.close();
}
// convert the buffer to chars
StringBuilder sb = new StringBuilder();
for (byte b : buf) {
char ch = (char) b;
if (ch == '\r' || ch == 0) {
// use MAC delimiter denotes end of text (added in the end in the processor below)
break;
} else {
sb.append(ch);
}
}
return sb.toString();
}
use of java.net.InetSocketAddress in project camel by apache.
the class Mina2Consumer method doStop.
@Override
protected void doStop() throws Exception {
if (configuration.isClientMode() && configuration.getProtocol().equals("tcp")) {
LOG.info("Disconnect from server address: {} using connector: {}", address, connector);
if (session != null) {
CloseFuture closeFuture = session.closeNow();
closeFuture.awaitUninterruptibly();
}
connector.dispose(true);
} else {
LOG.info("Unbinding from server address: {} using acceptor: {}", address, acceptor);
if (address instanceof InetSocketAddress) {
// need to check if the address is IPV4 all network address
if ("0.0.0.0".equals(((InetSocketAddress) address).getAddress().getHostAddress())) {
LOG.info("Unbind the server address {}", acceptor.getLocalAddresses());
acceptor.unbind(acceptor.getLocalAddresses());
} else {
acceptor.unbind(address);
}
} else {
acceptor.unbind(address);
}
}
super.doStop();
}
use of java.net.InetSocketAddress in project camel by apache.
the class Mina2Consumer method setupDatagramProtocol.
protected void setupDatagramProtocol(String uri, Mina2Configuration configuration) {
boolean minaLogger = configuration.isMinaLogger();
List<IoFilter> filters = configuration.getFilters();
address = new InetSocketAddress(configuration.getHost(), configuration.getPort());
acceptor = new NioDatagramAcceptor();
// acceptor connectorConfig
configureDataGramCodecFactory("Mina2Consumer", acceptor, configuration);
acceptor.setCloseOnDeactivation(true);
// reuse address is default true for datagram
if (configuration.isOrderedThreadPoolExecutor()) {
workerPool = new OrderedThreadPoolExecutor(configuration.getMaximumPoolSize());
} else {
workerPool = new UnorderedThreadPoolExecutor(configuration.getMaximumPoolSize());
}
acceptor.getFilterChain().addLast("threadPool", new ExecutorFilter(workerPool));
if (minaLogger) {
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
}
appendIoFiltersToChain(filters, acceptor.getFilterChain());
if (configuration.getSslContextParameters() != null) {
LOG.warn("Using datagram protocol, " + configuration.getProtocol() + ", but an SSLContextParameters instance was provided. SSLContextParameters is only supported on the TCP protocol.");
}
}
use of java.net.InetSocketAddress in project camel by apache.
the class Mina2TcpLineDelimiterUsingPlainSocketTest method sendAndReceive.
private String sendAndReceive(String input) throws IOException {
byte[] buf = new byte[128];
Socket soc = new Socket();
soc.connect(new InetSocketAddress("localhost", getPort()));
// Send message using plain Socket to test if this works
OutputStream os = null;
InputStream is = null;
try {
os = soc.getOutputStream();
// must append MAC newline at the end to flag end of textline to camel-mina2
os.write((input + "\r").getBytes());
is = soc.getInputStream();
int len = is.read(buf);
if (len == -1) {
// no data received
return null;
}
} finally {
IOHelper.close(is, os);
soc.close();
}
// convert the buffer to chars
StringBuilder sb = new StringBuilder();
for (byte b : buf) {
char ch = (char) b;
if (ch == '\r' || ch == 0) {
// use MAC delimiter denotes end of text (added in the end in the processor below)
break;
} else {
sb.append(ch);
}
}
return sb.toString();
}
Aggregations