use of org.apache.mina.transport.socket.SocketConnector in project camel by apache.
the class Mina2ProducerShutdownMockTest 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.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();
}
use of org.apache.mina.transport.socket.SocketConnector in project opennms by OpenNMS.
the class ConnectionFactoryNewConnectorImpl method connect.
/**
* <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;
}
Aggregations