Search in sources :

Example 1 with SocketConnector

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();
}
Also used : Exchange(org.apache.camel.Exchange) Field(java.lang.reflect.Field) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Producer(org.apache.camel.Producer) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SocketConnector(org.apache.mina.transport.socket.SocketConnector) Test(org.junit.Test)

Example 2 with SocketConnector

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;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ConnectFuture(org.apache.mina.core.future.ConnectFuture) NioSocketConnector(org.apache.mina.transport.socket.nio.NioSocketConnector) SocketConnector(org.apache.mina.transport.socket.SocketConnector)

Aggregations

SocketConnector (org.apache.mina.transport.socket.SocketConnector)2 Field (java.lang.reflect.Field)1 InetSocketAddress (java.net.InetSocketAddress)1 Endpoint (org.apache.camel.Endpoint)1 Exchange (org.apache.camel.Exchange)1 Producer (org.apache.camel.Producer)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 ConnectFuture (org.apache.mina.core.future.ConnectFuture)1 NioSocketConnector (org.apache.mina.transport.socket.nio.NioSocketConnector)1 Test (org.junit.Test)1