Search in sources :

Example 41 with ServiceActivatingHandler

use of org.springframework.integration.handler.ServiceActivatingHandler in project spring-integration by spring-projects.

the class UdpChannelAdapterTests method testUnicastReceiverException.

@Test
public void testUnicastReceiverException() throws Exception {
    SubscribableChannel channel = new DirectChannel();
    UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(0);
    adapter.setOutputChannel(channel);
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new FailingService());
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    channel.subscribe(handler);
    QueueChannel errorChannel = new QueueChannel();
    adapter.setErrorChannel(errorChannel);
    adapter.start();
    SocketTestUtils.waitListening(adapter);
    int port = adapter.getPort();
    Message<byte[]> message = MessageBuilder.withPayload("ABCD".getBytes()).build();
    DatagramPacketMessageMapper mapper = new DatagramPacketMessageMapper();
    DatagramPacket packet = mapper.fromMessage(message);
    packet.setSocketAddress(new InetSocketAddress("localhost", port));
    DatagramSocket datagramSocket = new DatagramSocket(0);
    datagramSocket.send(packet);
    datagramSocket.close();
    Message<?> receivedMessage = errorChannel.receive(10000);
    assertNotNull(receivedMessage);
    assertEquals("Failed", ((Exception) receivedMessage.getPayload()).getCause().getMessage());
    adapter.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) BeanFactory(org.springframework.beans.factory.BeanFactory) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 42 with ServiceActivatingHandler

use of org.springframework.integration.handler.ServiceActivatingHandler in project spring-integration by spring-projects.

the class TcpInboundGatewayTests method testNetClientMode.

@Test
public void testNetClientMode() throws Exception {
    final AtomicInteger port = new AtomicInteger();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final CountDownLatch latch3 = new CountDownLatch(1);
    final AtomicBoolean done = new AtomicBoolean();
    new SimpleAsyncTaskExecutor().execute(() -> {
        try {
            ServerSocket server = ServerSocketFactory.getDefault().createServerSocket(0, 10);
            port.set(server.getLocalPort());
            latch1.countDown();
            Socket socket = server.accept();
            socket.getOutputStream().write("Test1\r\nTest2\r\n".getBytes());
            byte[] bytes = new byte[12];
            readFully(socket.getInputStream(), bytes);
            assertEquals("Echo:Test1\r\n", new String(bytes));
            readFully(socket.getInputStream(), bytes);
            assertEquals("Echo:Test2\r\n", new String(bytes));
            latch2.await();
            socket.close();
            server.close();
            done.set(true);
            latch3.countDown();
        } catch (Exception e) {
            if (!done.get()) {
                e.printStackTrace();
            }
        }
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    AbstractClientConnectionFactory ccf = new TcpNetClientConnectionFactory("localhost", port.get());
    ccf.setSingleUse(false);
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(ccf);
    final QueueChannel channel = new QueueChannel();
    gateway.setRequestChannel(channel);
    gateway.setClientMode(true);
    gateway.setRetryInterval(10000);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.afterPropertiesSet();
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service());
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setPoolSize(1);
    taskScheduler.initialize();
    gateway.setTaskScheduler(taskScheduler);
    gateway.start();
    Message<?> message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    message = channel.receive(10000);
    assertNotNull(message);
    handler.handleMessage(message);
    latch2.countDown();
    assertTrue(latch3.await(10, TimeUnit.SECONDS));
    assertTrue(done.get());
    gateway.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) SimpleAsyncTaskExecutor(org.springframework.core.task.SimpleAsyncTaskExecutor) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) TcpNetClientConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetClientConnectionFactory) IOException(java.io.IOException) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) AbstractClientConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeanFactory(org.springframework.beans.factory.BeanFactory) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 43 with ServiceActivatingHandler

use of org.springframework.integration.handler.ServiceActivatingHandler in project spring-integration by spring-projects.

the class TcpInboundGatewayTests method testNetSingle.

@Test
public void testNetSingle() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    scf.setSingleUse(true);
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(scf);
    gateway.setBeanFactory(mock(BeanFactory.class));
    scf.start();
    TestingUtilities.waitListening(scf, 20000L);
    int port = scf.getPort();
    final QueueChannel channel = new QueueChannel();
    gateway.setRequestChannel(channel);
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service());
    handler.setChannelResolver(channelName -> channel);
    Socket socket1 = SocketFactory.getDefault().createSocket("localhost", port);
    socket1.getOutputStream().write("Test1\r\n".getBytes());
    Socket socket2 = SocketFactory.getDefault().createSocket("localhost", port);
    socket2.getOutputStream().write("Test2\r\n".getBytes());
    handler.handleMessage(channel.receive(10000));
    handler.handleMessage(channel.receive(10000));
    byte[] bytes = new byte[12];
    readFully(socket1.getInputStream(), bytes);
    assertEquals("Echo:Test1\r\n", new String(bytes));
    readFully(socket2.getInputStream(), bytes);
    assertEquals("Echo:Test2\r\n", new String(bytes));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 44 with ServiceActivatingHandler

use of org.springframework.integration.handler.ServiceActivatingHandler in project spring-integration by spring-projects.

the class TcpInboundGatewayTests method testNetNotSingle.

@Test
public void testNetNotSingle() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNetServerConnectionFactory(0);
    scf.setSingleUse(false);
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, 20000L);
    int port = scf.getPort();
    final QueueChannel channel = new QueueChannel();
    gateway.setRequestChannel(channel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service());
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test1\r\n".getBytes());
    socket.getOutputStream().write("Test2\r\n".getBytes());
    handler.handleMessage(channel.receive(10000));
    handler.handleMessage(channel.receive(10000));
    byte[] bytes = new byte[12];
    readFully(socket.getInputStream(), bytes);
    assertEquals("Echo:Test1\r\n", new String(bytes));
    readFully(socket.getInputStream(), bytes);
    assertEquals("Echo:Test2\r\n", new String(bytes));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNetServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 45 with ServiceActivatingHandler

use of org.springframework.integration.handler.ServiceActivatingHandler in project spring-integration by spring-projects.

the class TcpInboundGatewayTests method testNioNotSingle.

@Test
public void testNioNotSingle() throws Exception {
    AbstractServerConnectionFactory scf = new TcpNioServerConnectionFactory(0);
    scf.setSingleUse(false);
    TcpInboundGateway gateway = new TcpInboundGateway();
    gateway.setConnectionFactory(scf);
    scf.start();
    TestingUtilities.waitListening(scf, 20000L);
    int port = scf.getPort();
    final QueueChannel channel = new QueueChannel();
    gateway.setRequestChannel(channel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    ServiceActivatingHandler handler = new ServiceActivatingHandler(new Service());
    Socket socket = SocketFactory.getDefault().createSocket("localhost", port);
    socket.getOutputStream().write("Test1\r\n".getBytes());
    socket.getOutputStream().write("Test2\r\n".getBytes());
    handler.handleMessage(channel.receive(10000));
    handler.handleMessage(channel.receive(10000));
    Set<String> results = new HashSet<String>();
    byte[] bytes = new byte[12];
    readFully(socket.getInputStream(), bytes);
    results.add(new String(bytes));
    readFully(socket.getInputStream(), bytes);
    results.add(new String(bytes));
    assertTrue(results.remove("Echo:Test1\r\n"));
    assertTrue(results.remove("Echo:Test2\r\n"));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ServiceActivatingHandler (org.springframework.integration.handler.ServiceActivatingHandler)45 Test (org.junit.Test)38 QueueChannel (org.springframework.integration.channel.QueueChannel)29 BeanFactory (org.springframework.beans.factory.BeanFactory)10 DirectChannel (org.springframework.integration.channel.DirectChannel)10 GenericMessage (org.springframework.messaging.support.GenericMessage)10 Message (org.springframework.messaging.Message)8 ServerSocket (java.net.ServerSocket)7 Socket (java.net.Socket)7 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)6 AbstractServerConnectionFactory (org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory)6 RequestReplyExchanger (org.springframework.integration.gateway.RequestReplyExchanger)5 Date (java.util.Date)4 TcpNetServerConnectionFactory (org.springframework.integration.ip.tcp.connection.TcpNetServerConnectionFactory)4 PollableChannel (org.springframework.messaging.PollableChannel)4 IOException (java.io.IOException)3 PublishSubscribeChannel (org.springframework.integration.channel.PublishSubscribeChannel)3 TestUtils (org.springframework.integration.test.util.TestUtils)3 SubscribableChannel (org.springframework.messaging.SubscribableChannel)3 HashMap (java.util.HashMap)2