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();
}
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();
}
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));
}
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();
}
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));
}
Aggregations