Search in sources :

Example 81 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration by spring-projects.

the class StompServerIntegrationTests method testStompAdapters.

@Test
public void testStompAdapters() throws Exception {
    ConfigurableApplicationContext context1 = new AnnotationConfigApplicationContext(ContextConfiguration.class);
    ConfigurableApplicationContext context2 = new AnnotationConfigApplicationContext(ContextConfiguration.class);
    PollableChannel stompEvents1 = context1.getBean("stompEvents", PollableChannel.class);
    PollableChannel stompEvents2 = context2.getBean("stompEvents", PollableChannel.class);
    PollableChannel stompInputChannel1 = context1.getBean("stompInputChannel", PollableChannel.class);
    PollableChannel stompInputChannel2 = context2.getBean("stompInputChannel", PollableChannel.class);
    MessageChannel stompOutputChannel1 = context1.getBean("stompOutputChannel", MessageChannel.class);
    MessageChannel stompOutputChannel2 = context2.getBean("stompOutputChannel", MessageChannel.class);
    Message<?> eventMessage;
    do {
        eventMessage = stompEvents1.receive(10000);
    } while (eventMessage != null && !(eventMessage.getPayload() instanceof StompSessionConnectedEvent));
    assertNotNull(eventMessage);
    eventMessage = stompEvents1.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    StompReceiptEvent stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SUBSCRIBE, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompSessionConnectedEvent.class));
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SUBSCRIBE, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    stompOutputChannel1.send(new GenericMessage<byte[]>("Hello, Client#2!".getBytes()));
    Message<?> receive11 = stompInputChannel1.receive(10000);
    Message<?> receive21 = stompInputChannel2.receive(10000);
    assertNotNull(receive11);
    assertNotNull(receive21);
    assertArrayEquals("Hello, Client#2!".getBytes(), (byte[]) receive11.getPayload());
    assertArrayEquals("Hello, Client#2!".getBytes(), (byte[]) receive21.getPayload());
    stompOutputChannel2.send(new GenericMessage<byte[]>("Hello, Client#1!".getBytes()));
    Message<?> receive12 = stompInputChannel1.receive(10000);
    Message<?> receive22 = stompInputChannel2.receive(10000);
    assertNotNull(receive12);
    assertNotNull(receive22);
    assertArrayEquals("Hello, Client#1!".getBytes(), (byte[]) receive12.getPayload());
    assertArrayEquals("Hello, Client#1!".getBytes(), (byte[]) receive22.getPayload());
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SEND, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    assertArrayEquals("Hello, Client#1!".getBytes(), (byte[]) stompReceiptEvent.getMessage().getPayload());
    Lifecycle stompInboundChannelAdapter2 = context2.getBean("stompInboundChannelAdapter", Lifecycle.class);
    stompInboundChannelAdapter2.stop();
    stompOutputChannel1.send(new GenericMessage<byte[]>("How do you do?".getBytes()));
    Message<?> receive13 = stompInputChannel1.receive(10000);
    assertNotNull(receive13);
    Message<?> receive23 = stompInputChannel2.receive(100);
    assertNull(receive23);
    stompInboundChannelAdapter2.start();
    eventMessage = stompEvents2.receive(10000);
    assertNotNull(eventMessage);
    assertThat(eventMessage.getPayload(), instanceOf(StompReceiptEvent.class));
    stompReceiptEvent = (StompReceiptEvent) eventMessage.getPayload();
    assertEquals(StompCommand.SUBSCRIBE, stompReceiptEvent.getStompCommand());
    assertEquals("/topic/myTopic", stompReceiptEvent.getDestination());
    stompOutputChannel1.send(new GenericMessage<byte[]>("???".getBytes()));
    Message<?> receive24 = stompInputChannel2.receive(10000);
    assertNotNull(receive24);
    assertArrayEquals("???".getBytes(), (byte[]) receive24.getPayload());
    activeMQBroker.stop();
    do {
        eventMessage = stompEvents1.receive(10000);
        assertNotNull(eventMessage);
    } while (!(eventMessage.getPayload() instanceof StompConnectionFailedEvent));
    try {
        stompOutputChannel1.send(new GenericMessage<byte[]>("foo".getBytes()));
        fail("MessageDeliveryException is expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getMessage(), containsString("could not deliver message"));
    }
    activeMQBroker.start(false);
    do {
        eventMessage = stompEvents1.receive(20000);
        assertNotNull(eventMessage);
    } while (!(eventMessage.getPayload() instanceof StompReceiptEvent));
    do {
        eventMessage = stompEvents2.receive(10000);
        assertNotNull(eventMessage);
    } while (!(eventMessage.getPayload() instanceof StompReceiptEvent));
    stompOutputChannel1.send(new GenericMessage<byte[]>("foo".getBytes()));
    Message<?> receive25 = stompInputChannel2.receive(10000);
    assertNotNull(receive25);
    assertArrayEquals("foo".getBytes(), (byte[]) receive25.getPayload());
    context1.close();
    context2.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) StompReceiptEvent(org.springframework.integration.stomp.event.StompReceiptEvent) StompConnectionFailedEvent(org.springframework.integration.stomp.event.StompConnectionFailedEvent) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) Lifecycle(org.springframework.context.Lifecycle) PollableChannel(org.springframework.messaging.PollableChannel) StompSessionConnectedEvent(org.springframework.integration.stomp.event.StompSessionConnectedEvent) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 82 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration by spring-projects.

the class SyslogReceivingChannelAdapterTests method testAsMapFalse.

@Test
public void testAsMapFalse() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(SyslogReceivingChannelAdapterFactoryBean.Protocol.udp);
    factory.setPort(0);
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    factory.setBeanFactory(mock(BeanFactory.class));
    factory.afterPropertiesSet();
    factory.start();
    UnicastReceivingChannelAdapter server = TestUtils.getPropertyValue(factory, "adapter.udpAdapter", UnicastReceivingChannelAdapter.class);
    TestingUtilities.waitListening(server, null);
    UdpSyslogReceivingChannelAdapter adapter = (UdpSyslogReceivingChannelAdapter) factory.getObject();
    DefaultMessageConverter defaultMessageConverter = new DefaultMessageConverter();
    defaultMessageConverter.setAsMap(false);
    adapter.setConverter(defaultMessageConverter);
    Thread.sleep(1000);
    byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE".getBytes("UTF-8");
    DatagramPacket packet = new DatagramPacket(buf, buf.length, new InetSocketAddress("localhost", adapter.getPort()));
    DatagramSocket socket = new DatagramSocket();
    socket.send(packet);
    socket.close();
    Message<?> message = outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("WEBERN", message.getHeaders().get("syslog_HOST"));
    assertEquals("<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE", new String((byte[]) message.getPayload(), "UTF-8"));
    adapter.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) InetSocketAddress(java.net.InetSocketAddress) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UnicastReceivingChannelAdapter(org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter) SyslogReceivingChannelAdapterFactoryBean(org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean) DefaultMessageConverter(org.springframework.integration.syslog.DefaultMessageConverter) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 83 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration by spring-projects.

the class SyslogReceivingChannelAdapterTests method testTcpRFC5424.

@Test
public void testTcpRFC5424() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(SyslogReceivingChannelAdapterFactoryBean.Protocol.tcp);
    PollableChannel outputChannel = new QueueChannel();
    factory.setOutputChannel(outputChannel);
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    final CountDownLatch latch = new CountDownLatch(2);
    doAnswer(invocation -> {
        latch.countDown();
        return null;
    }).when(publisher).publishEvent(any(ApplicationEvent.class));
    factory.setBeanFactory(mock(BeanFactory.class));
    AbstractServerConnectionFactory connectionFactory = new TcpNioServerConnectionFactory(0);
    connectionFactory.setDeserializer(new RFC6587SyslogDeserializer());
    connectionFactory.setApplicationEventPublisher(publisher);
    factory.setConnectionFactory(connectionFactory);
    factory.setConverter(new RFC5424MessageConverter());
    factory.afterPropertiesSet();
    factory.start();
    TestingUtilities.waitListening(connectionFactory, null);
    TcpSyslogReceivingChannelAdapter adapter = (TcpSyslogReceivingChannelAdapter) factory.getObject();
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    doReturn(true).when(logger).isDebugEnabled();
    final CountDownLatch sawLog = new CountDownLatch(1);
    doAnswer(invocation -> {
        if (((String) invocation.getArgument(0)).contains("Error on syslog socket")) {
            sawLog.countDown();
        }
        invocation.callRealMethod();
        return null;
    }).when(logger).debug(anyString());
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    Thread.sleep(1000);
    byte[] buf = ("253 <14>1 2014-06-20T09:14:07+00:00 loggregator d0602076-b14a-4c55-852a-981e7afeed38 DEA - " + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"]" + "[exampleSDID@32473 iut=\\\"3\\\" eventSource=\\\"Application\\\" eventID=\\\"1011\\\"] Removing instance").getBytes("UTF-8");
    Socket socket = SocketFactory.getDefault().createSocket("localhost", connectionFactory.getPort());
    socket.getOutputStream().write(buf);
    socket.close();
    assertTrue(sawLog.await(10, TimeUnit.SECONDS));
    @SuppressWarnings("unchecked") Message<Map<String, ?>> message = (Message<Map<String, ?>>) outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("loggregator", message.getPayload().get("syslog_HOST"));
    adapter.stop();
    assertTrue(latch.await(10, TimeUnit.SECONDS));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) RFC5424MessageConverter(org.springframework.integration.syslog.RFC5424MessageConverter) Message(org.springframework.messaging.Message) TcpNioServerConnectionFactory(org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory) Log(org.apache.commons.logging.Log) ApplicationEvent(org.springframework.context.ApplicationEvent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) SyslogReceivingChannelAdapterFactoryBean(org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractServerConnectionFactory(org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory) Map(java.util.Map) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) Test(org.junit.Test)

Example 84 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration by spring-projects.

the class SyslogReceivingChannelAdapterTests method testUdp.

@Test
public void testUdp() throws Exception {
    SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean(SyslogReceivingChannelAdapterFactoryBean.Protocol.udp);
    PollableChannel outputChannel = new QueueChannel();
    factory.setPort(0);
    factory.setOutputChannel(outputChannel);
    factory.setBeanFactory(mock(BeanFactory.class));
    factory.afterPropertiesSet();
    factory.start();
    UnicastReceivingChannelAdapter server = TestUtils.getPropertyValue(factory, "adapter.udpAdapter", UnicastReceivingChannelAdapter.class);
    TestingUtilities.waitListening(server, null);
    UdpSyslogReceivingChannelAdapter adapter = (UdpSyslogReceivingChannelAdapter) factory.getObject();
    byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE".getBytes("UTF-8");
    DatagramPacket packet = new DatagramPacket(buf, buf.length, new InetSocketAddress("localhost", server.getPort()));
    DatagramSocket socket = new DatagramSocket();
    socket.send(packet);
    socket.close();
    Message<?> message = outputChannel.receive(10000);
    assertNotNull(message);
    assertEquals("WEBERN", message.getHeaders().get("syslog_HOST"));
    adapter.stop();
}
Also used : SyslogReceivingChannelAdapterFactoryBean(org.springframework.integration.syslog.config.SyslogReceivingChannelAdapterFactoryBean) QueueChannel(org.springframework.integration.channel.QueueChannel) DatagramSocket(java.net.DatagramSocket) InetSocketAddress(java.net.InetSocketAddress) DatagramPacket(java.net.DatagramPacket) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) UnicastReceivingChannelAdapter(org.springframework.integration.ip.udp.UnicastReceivingChannelAdapter) Test(org.junit.Test)

Example 85 with PollableChannel

use of org.springframework.messaging.PollableChannel in project spring-integration by spring-projects.

the class XPathFilterParserTests method stringExpressionRegex.

@Test
public void stringExpressionRegex() throws Exception {
    MessageChannel inputChannel = context.getBean("stringFilterRegexInput", MessageChannel.class);
    QueueChannel replyChannel = new QueueChannel();
    PollableChannel discardChannel = context.getBean("stringFilterRegexRejections", PollableChannel.class);
    Document docToAccept1 = XmlTestUtil.getDocumentForString("<name>aBcDeFgHiJk</name>");
    Document docToAccept2 = XmlTestUtil.getDocumentForString("<name>xyz</name>");
    Document docToReject = XmlTestUtil.getDocumentForString("<name>abc123</name>");
    Message<?> shouldBeAccepted1 = MessageBuilder.withPayload(docToAccept1).setReplyChannel(replyChannel).build();
    Message<?> shouldBeAccepted2 = MessageBuilder.withPayload(docToAccept2).setReplyChannel(replyChannel).build();
    Message<?> shouldBeRejected = MessageBuilder.withPayload(docToReject).setReplyChannel(replyChannel).build();
    inputChannel.send(shouldBeAccepted1);
    inputChannel.send(shouldBeAccepted2);
    inputChannel.send(shouldBeRejected);
    assertEquals(shouldBeAccepted1, replyChannel.receive(0));
    assertEquals(shouldBeAccepted2, replyChannel.receive(0));
    assertEquals(shouldBeRejected, discardChannel.receive(0));
    assertNull(replyChannel.receive(0));
    assertNull(discardChannel.receive(0));
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) PollableChannel(org.springframework.messaging.PollableChannel) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

PollableChannel (org.springframework.messaging.PollableChannel)210 Test (org.junit.Test)190 MessageChannel (org.springframework.messaging.MessageChannel)89 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)82 QueueChannel (org.springframework.integration.channel.QueueChannel)52 GenericMessage (org.springframework.messaging.support.GenericMessage)52 Message (org.springframework.messaging.Message)40 BeanFactory (org.springframework.beans.factory.BeanFactory)25 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)20 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)19 MessagingException (org.springframework.messaging.MessagingException)16 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)13 MessagingTemplate (org.springframework.integration.core.MessagingTemplate)12 ErrorMessage (org.springframework.messaging.support.ErrorMessage)12 Document (org.w3c.dom.Document)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 Matchers.containsString (org.hamcrest.Matchers.containsString)11 Date (java.util.Date)10 ArrayList (java.util.ArrayList)9 MessageHistory (org.springframework.integration.history.MessageHistory)9