List of usage examples for org.springframework.messaging.support GenericMessage GenericMessage
public GenericMessage(T payload)
From source file:org.springframework.integration.channel.QueueChannelTests.java
@Test public void testReactorPersistentQueue() throws InterruptedException, IOException { final AtomicBoolean messageReceived = new AtomicBoolean(false); final CountDownLatch latch = new CountDownLatch(1); PersistentQueue<Message<?>> queue = new PersistentQueueSpec<Message<?>>() .codec(new JavaSerializationCodec<Message<?>>()) .basePath(this.tempFolder.getRoot().getAbsolutePath()).get(); final QueueChannel channel = new QueueChannel(queue); new Thread(new Runnable() { @Override// w w w .java 2 s . c o m public void run() { Message<?> message = channel.receive(); if (message != null) { messageReceived.set(true); latch.countDown(); } } }).start(); assertFalse(messageReceived.get()); channel.send(new GenericMessage<String>("testing")); latch.await(1000, TimeUnit.MILLISECONDS); assertTrue(messageReceived.get()); final CountDownLatch latch1 = new CountDownLatch(2); Thread thread = new Thread(new Runnable() { @Override public void run() { while (true) { Message<?> message = channel.receive(100); if (message != null) { latch1.countDown(); if (latch1.getCount() == 0) { break; } } } } }); thread.start(); Thread.sleep(200); channel.send(new GenericMessage<String>("testing")); channel.send(new GenericMessage<String>("testing")); assertTrue(latch1.await(1000, TimeUnit.MILLISECONDS)); final AtomicBoolean receiveInterrupted = new AtomicBoolean(false); final CountDownLatch latch2 = new CountDownLatch(1); Thread t = new Thread(new Runnable() { @Override public void run() { Message<?> message = channel.receive(10000); receiveInterrupted.set(true); assertTrue(message == null); latch2.countDown(); } }); t.start(); assertFalse(receiveInterrupted.get()); t.interrupt(); latch2.await(); assertTrue(receiveInterrupted.get()); receiveInterrupted.set(false); final CountDownLatch latch3 = new CountDownLatch(1); t = new Thread(new Runnable() { @Override public void run() { Message<?> message = channel.receive(); receiveInterrupted.set(true); assertTrue(message == null); latch3.countDown(); } }); t.start(); assertFalse(receiveInterrupted.get()); t.interrupt(); latch3.await(); assertTrue(receiveInterrupted.get()); GenericMessage<String> message1 = new GenericMessage<String>("test1"); GenericMessage<String> message2 = new GenericMessage<String>("test2"); assertTrue(channel.send(message1)); assertTrue(channel.send(message2)); List<Message<?>> clearedMessages = channel.clear(); assertNotNull(clearedMessages); assertEquals(2, clearedMessages.size()); clearedMessages = channel.clear(); assertNotNull(clearedMessages); assertEquals(0, clearedMessages.size()); // Test on artificial infinite wait // channel.receive(); // Distributed scenario final CountDownLatch latch4 = new CountDownLatch(1); new Thread(new Runnable() { @Override public void run() { Message<?> message = channel.receive(); if (message != null) { latch4.countDown(); } } }).start(); queue.add(new GenericMessage<String>("foo")); assertTrue(latch4.await(1000, TimeUnit.MILLISECONDS)); }
From source file:org.springframework.integration.config.SourcePollingChannelAdapterFactoryBeanTests.java
@Test public void testAdviceChain() throws Exception { SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean(); QueueChannel outputChannel = new QueueChannel(); TestApplicationContext context = TestUtils.createTestApplicationContext(); factoryBean.setBeanFactory(context.getBeanFactory()); factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader()); factoryBean.setOutputChannel(outputChannel); factoryBean.setSource(() -> new GenericMessage<>("test")); PollerMetadata pollerMetadata = new PollerMetadata(); List<Advice> adviceChain = new ArrayList<Advice>(); final AtomicBoolean adviceApplied = new AtomicBoolean(false); adviceChain.add((MethodInterceptor) invocation -> { adviceApplied.set(true);/*from w w w . j a v a 2 s. c o m*/ return invocation.proceed(); }); pollerMetadata.setTrigger(new PeriodicTrigger(5000)); pollerMetadata.setMaxMessagesPerPoll(1); pollerMetadata.setAdviceChain(adviceChain); factoryBean.setPollerMetadata(pollerMetadata); factoryBean.setAutoStartup(true); factoryBean.afterPropertiesSet(); context.registerEndpoint("testPollingEndpoint", factoryBean.getObject()); context.refresh(); Message<?> message = outputChannel.receive(5000); assertEquals("test", message.getPayload()); assertTrue("adviceChain was not applied", adviceApplied.get()); }
From source file:org.springframework.integration.config.SourcePollingChannelAdapterFactoryBeanTests.java
@Test public void testTransactionalAdviceChain() throws Throwable { SourcePollingChannelAdapterFactoryBean factoryBean = new SourcePollingChannelAdapterFactoryBean(); QueueChannel outputChannel = new QueueChannel(); TestApplicationContext context = TestUtils.createTestApplicationContext(); factoryBean.setBeanFactory(context.getBeanFactory()); factoryBean.setBeanClassLoader(ClassUtils.getDefaultClassLoader()); factoryBean.setOutputChannel(outputChannel); factoryBean.setSource(() -> new GenericMessage<>("test")); PollerMetadata pollerMetadata = new PollerMetadata(); List<Advice> adviceChain = new ArrayList<Advice>(); final AtomicBoolean adviceApplied = new AtomicBoolean(false); adviceChain.add((MethodInterceptor) invocation -> { adviceApplied.set(true);/*w ww . j ava 2s .c o m*/ return invocation.proceed(); }); pollerMetadata.setTrigger(new PeriodicTrigger(5000)); pollerMetadata.setMaxMessagesPerPoll(1); final AtomicInteger count = new AtomicInteger(); final MethodInterceptor txAdvice = mock(MethodInterceptor.class); adviceChain.add((MethodInterceptor) invocation -> { count.incrementAndGet(); return invocation.proceed(); }); when(txAdvice.invoke(any(MethodInvocation.class))).thenAnswer(invocation -> { count.incrementAndGet(); return ((MethodInvocation) invocation.getArgument(0)).proceed(); }); pollerMetadata.setAdviceChain(adviceChain); factoryBean.setPollerMetadata(pollerMetadata); factoryBean.setAutoStartup(true); factoryBean.afterPropertiesSet(); context.registerEndpoint("testPollingEndpoint", factoryBean.getObject()); context.refresh(); Message<?> message = outputChannel.receive(5000); assertEquals("test", message.getPayload()); assertEquals(1, count.get()); assertTrue("adviceChain was not applied", adviceApplied.get()); }
From source file:org.springframework.integration.config.xml.GatewayParserTests.java
@Test public void testSolicitResponse() { PollableChannel channel = (PollableChannel) context.getBean("replyChannel"); channel.send(new GenericMessage<String>("foo")); TestService service = (TestService) context.getBean("solicitResponse"); String result = service.solicitResponse(); assertEquals("foo", result); }
From source file:org.springframework.integration.configuration.EnableIntegrationTests.java
@Test public void testAnnotatedServiceActivator() throws Exception { this.serviceActivatorEndpoint.setReceiveTimeout(10000); this.serviceActivatorEndpoint.start(); assertTrue(this.inputReceiveLatch.await(10, TimeUnit.SECONDS)); assertEquals(10L, TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "maxMessagesPerPoll")); Trigger trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class)); assertEquals(100L, TestUtils.getPropertyValue(trigger, "period")); assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)); assertTrue(this.annotationTestService.isRunning()); Log logger = spy(TestUtils.getPropertyValue(this.serviceActivatorEndpoint, "logger", Log.class)); when(logger.isDebugEnabled()).thenReturn(true); final CountDownLatch pollerInterruptedLatch = new CountDownLatch(1); doAnswer(invocation -> {/*from w ww. ja v a 2 s . com*/ pollerInterruptedLatch.countDown(); invocation.callRealMethod(); return null; }).when(logger).debug("Received no Message during the poll, returning 'false'"); new DirectFieldAccessor(this.serviceActivatorEndpoint).setPropertyValue("logger", logger); this.serviceActivatorEndpoint.stop(); assertFalse(this.annotationTestService.isRunning()); // wait until the service activator's poller is interrupted. assertTrue(pollerInterruptedLatch.await(10, TimeUnit.SECONDS)); this.serviceActivatorEndpoint.start(); assertTrue(this.annotationTestService.isRunning()); trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint1, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class)); assertEquals(100L, TestUtils.getPropertyValue(trigger, "period")); assertTrue(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)); trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint2, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(CronTrigger.class)); assertEquals("0 5 7 * * *", TestUtils.getPropertyValue(trigger, "sequenceGenerator.expression")); trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint3, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class)); assertEquals(11L, TestUtils.getPropertyValue(trigger, "period")); assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)); trigger = TestUtils.getPropertyValue(this.serviceActivatorEndpoint4, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class)); assertEquals(1000L, TestUtils.getPropertyValue(trigger, "period")); assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)); assertSame(this.myTrigger, trigger); trigger = TestUtils.getPropertyValue(this.transformer, "trigger", Trigger.class); assertThat(trigger, Matchers.instanceOf(PeriodicTrigger.class)); assertEquals(10L, TestUtils.getPropertyValue(trigger, "period")); assertFalse(TestUtils.getPropertyValue(trigger, "fixedRate", Boolean.class)); this.input.send(MessageBuilder.withPayload("Foo").build()); Message<?> interceptedMessage = this.wireTapChannel.receive(10000); assertNotNull(interceptedMessage); assertEquals("Foo", interceptedMessage.getPayload()); Message<?> receive = this.output.receive(10000); assertNotNull(receive); assertEquals("FOO", receive.getPayload()); receive = this.wireTapFromOutput.receive(10000); assertNotNull(receive); assertEquals("FOO", receive.getPayload()); MessageHistory messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class); assertNotNull(messageHistory); String messageHistoryString = messageHistory.toString(); assertThat(messageHistoryString, Matchers.containsString("input")); assertThat(messageHistoryString, Matchers.containsString("annotationTestService.handle.serviceActivator.handler")); assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output"))); receive = this.publishedChannel.receive(10000); assertNotNull(receive); assertEquals("foo", receive.getPayload()); messageHistory = receive.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class); assertNotNull(messageHistory); messageHistoryString = messageHistory.toString(); assertThat(messageHistoryString, Matchers.not(Matchers.containsString("input"))); assertThat(messageHistoryString, Matchers.not(Matchers.containsString("output"))); assertThat(messageHistoryString, Matchers.containsString("publishedChannel")); assertNull(this.wireTapChannel.receive(0)); assertThat(this.testChannelInterceptor.getInvoked(), Matchers.greaterThan(0)); assertThat(this.fbInterceptorCounter.get(), Matchers.greaterThan(0)); assertTrue(this.context.containsBean("annotationTestService.count.inboundChannelAdapter.source")); Object messageSource = this.context.getBean("annotationTestService.count.inboundChannelAdapter.source"); assertThat(messageSource, Matchers.instanceOf(MethodInvokingMessageSource.class)); assertNull(this.counterChannel.receive(10)); SmartLifecycle countSA = this.context.getBean("annotationTestService.count.inboundChannelAdapter", SmartLifecycle.class); assertFalse(countSA.isAutoStartup()); assertEquals(23, countSA.getPhase()); countSA.start(); for (int i = 0; i < 10; i++) { Message<?> message = this.counterChannel.receive(1000); assertNotNull(message); assertEquals(i + 1, message.getPayload()); } Message<?> message = this.fooChannel.receive(1000); assertNotNull(message); assertEquals("foo", message.getPayload()); message = this.fooChannel.receive(1000); assertNotNull(message); assertEquals("foo", message.getPayload()); assertNull(this.fooChannel.receive(10)); message = this.messageChannel.receive(1000); assertNotNull(message); assertEquals("bar", message.getPayload()); assertTrue(message.getHeaders().containsKey("foo")); assertEquals("FOO", message.getHeaders().get("foo")); MessagingTemplate messagingTemplate = new MessagingTemplate(this.controlBusChannel); assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class)); this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.start()")); assertTrue(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class)); this.controlBusChannel.send(new GenericMessage<String>("@lifecycle.stop()")); assertFalse(messagingTemplate.convertSendAndReceive("@lifecycle.isRunning()", Boolean.class)); }
From source file:org.springframework.integration.configuration.EnableIntegrationTests.java
@Test public void testIntegrationConverter() { this.numberChannel.send(new GenericMessage<Integer>(10)); this.numberChannel.send(new GenericMessage<Boolean>(true)); assertThat(this.testConverter.getInvoked(), Matchers.greaterThan(0)); assertTrue(this.bytesChannel.send(new GenericMessage<byte[]>("foo".getBytes()))); assertTrue(this.bytesChannel.send(new GenericMessage<>(MutableMessageBuilder.withPayload("").build()))); }
From source file:org.springframework.integration.configuration.EnableIntegrationTests.java
@Test public void testBridgeAnnotations() { GenericMessage<?> testMessage = new GenericMessage<Object>("foo"); this.bridgeInput.send(testMessage); Message<?> receive = this.bridgeOutput.receive(2000); assertNotNull(receive);//www .java2 s .c om assertSame(receive, testMessage); assertNull(this.bridgeOutput.receive(10)); this.pollableBridgeInput.send(testMessage); receive = this.pollableBridgeOutput.receive(2000); assertNotNull(receive); assertSame(receive, testMessage); assertNull(this.pollableBridgeOutput.receive(10)); try { this.metaBridgeInput.send(testMessage); fail("MessageDeliveryException expected"); } catch (Exception e) { assertThat(e, Matchers.instanceOf(MessageDeliveryException.class)); assertThat(e.getMessage(), Matchers.containsString("Dispatcher has no subscribers")); } this.context .getBean("enableIntegrationTests.ContextConfiguration.metaBridgeOutput.bridgeFrom", Lifecycle.class) .start(); this.metaBridgeInput.send(testMessage); receive = this.metaBridgeOutput.receive(2000); assertNotNull(receive); assertSame(receive, testMessage); assertNull(this.metaBridgeOutput.receive(10)); this.bridgeToInput.send(testMessage); receive = this.bridgeToOutput.receive(2000); assertNotNull(receive); assertSame(receive, testMessage); assertNull(this.bridgeToOutput.receive(10)); PollableChannel replyChannel = new QueueChannel(); Message<?> bridgeMessage = MessageBuilder.fromMessage(testMessage).setReplyChannel(replyChannel).build(); this.pollableBridgeToInput.send(bridgeMessage); receive = replyChannel.receive(2000); assertNotNull(receive); assertSame(receive, bridgeMessage); assertNull(replyChannel.receive(10)); try { this.myBridgeToInput.send(testMessage); fail("MessageDeliveryException expected"); } catch (Exception e) { assertThat(e, Matchers.instanceOf(MessageDeliveryException.class)); assertThat(e.getMessage(), Matchers.containsString("Dispatcher has no subscribers")); } this.context .getBean("enableIntegrationTests.ContextConfiguration.myBridgeToInput.bridgeTo", Lifecycle.class) .start(); this.myBridgeToInput.send(bridgeMessage); receive = replyChannel.receive(2000); assertNotNull(receive); assertSame(receive, bridgeMessage); assertNull(replyChannel.receive(10)); }
From source file:org.springframework.integration.core.MessageIdGenerationTests.java
@Test public void testCustomIdGenerationWithParentRegistrar() throws Exception { ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext( "MessageIdGenerationTests-context-withGenerator.xml", this.getClass()); ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext( new String[] { "MessageIdGenerationTests-context.xml" }, this.getClass(), parent); IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); MessageChannel inputChannel = child.getBean("input", MessageChannel.class); inputChannel.send(new GenericMessage<Integer>(0)); verify(idGenerator, atLeastOnce()).generateId(); child.close();//from www .j ava 2 s. com parent.close(); this.assertDestroy(); }
From source file:org.springframework.integration.core.MessageIdGenerationTests.java
@Test public void testCustomIdGenerationWithParentChildIndependentCreation() throws Exception { ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext( "MessageIdGenerationTests-context-withGenerator.xml", this.getClass()); GenericXmlApplicationContext child = new GenericXmlApplicationContext(); child.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context.xml"); child.setParent(parent);// w w w. ja v a 2s . com child.refresh(); IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); MessageChannel inputChannel = child.getBean("input", MessageChannel.class); inputChannel.send(new GenericMessage<Integer>(0)); verify(idGenerator, atLeastOnce()).generateId(); child.close(); parent.close(); this.assertDestroy(); }
From source file:org.springframework.integration.core.MessageIdGenerationTests.java
@Test public void testCustomIdGenerationWithParentRegistrarClosed() throws Exception { ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext( "MessageIdGenerationTests-context-withGenerator.xml", this.getClass()); ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext( new String[] { "MessageIdGenerationTests-context.xml" }, this.getClass(), parent); IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class); MessageChannel inputChannel = child.getBean("input", MessageChannel.class); inputChannel.send(new GenericMessage<Integer>(0)); verify(idGenerator, atLeastOnce()).generateId(); parent.close();//from w w w . jav a 2 s.c o m child.close(); this.assertDestroy(); }