private Collection<StepExecution> receiveReplies(PollableChannel currentReplyChannel) { @SuppressWarnings("unchecked") Message<Collection<StepExecution>> message = (Message<Collection<StepExecution>>) messagingGateway.receive(currentReplyChannel); if(message == null) { throw new MessageTimeoutException("Timeout occurred before all partitions returned"); } else if (logger.isDebugEnabled()) { logger.debug("Received replies: " + message); } return message.getPayload(); }
Message<ChunkResponse> message = (Message<ChunkResponse>) messagingGateway.receive(replyChannel); if (message != null) { ChunkResponse payload = message.getPayload();
@SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testHandleNoReply() throws Exception { //execute with no default set messageChannelPartitionHandler = new MessageChannelPartitionHandler(); //mock StepExecution masterStepExecution = mock(StepExecution.class); StepExecutionSplitter stepExecutionSplitter = mock(StepExecutionSplitter.class); MessagingTemplate operations = mock(MessagingTemplate.class); Message message = mock(Message.class); //when HashSet<StepExecution> stepExecutions = new HashSet<>(); stepExecutions.add(new StepExecution("step1", new JobExecution(5L))); when(stepExecutionSplitter.split(any(StepExecution.class), eq(1))).thenReturn(stepExecutions); when(message.getPayload()).thenReturn(Collections.emptyList()); when(operations.receive((PollableChannel) any())).thenReturn(message); //set messageChannelPartitionHandler.setMessagingOperations(operations); //execute Collection<StepExecution> executions = messageChannelPartitionHandler.handle(stepExecutionSplitter, masterStepExecution); //verify assertNotNull(executions); assertTrue(executions.isEmpty()); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testHandleWithReplyChannel() throws Exception { //execute with no default set messageChannelPartitionHandler = new MessageChannelPartitionHandler(); //mock StepExecution masterStepExecution = mock(StepExecution.class); StepExecutionSplitter stepExecutionSplitter = mock(StepExecutionSplitter.class); MessagingTemplate operations = mock(MessagingTemplate.class); Message message = mock(Message.class); PollableChannel replyChannel = mock(PollableChannel.class); //when HashSet<StepExecution> stepExecutions = new HashSet<>(); stepExecutions.add(new StepExecution("step1", new JobExecution(5L))); when(stepExecutionSplitter.split(any(StepExecution.class), eq(1))).thenReturn(stepExecutions); when(message.getPayload()).thenReturn(Collections.emptyList()); when(operations.receive(replyChannel)).thenReturn(message); //set messageChannelPartitionHandler.setMessagingOperations(operations); messageChannelPartitionHandler.setReplyChannel(replyChannel); //execute Collection<StepExecution> executions = messageChannelPartitionHandler.handle(stepExecutionSplitter, masterStepExecution); //verify assertNotNull(executions); assertTrue(executions.isEmpty()); }
@Nullable protected Message<?> receiveMessage(long timeout) { initializeIfNecessary(); MessageChannel channel = getReplyChannel(); Assert.state(channel instanceof PollableChannel, "receive is not supported, because no pollable reply channel has been configured"); return this.messagingTemplate.receive(channel, timeout); }
@Nullable protected Message<?> receiveMessage() { initializeIfNecessary(); MessageChannel channel = getReplyChannel(); Assert.state(channel instanceof PollableChannel, "receive is not supported, because no pollable reply channel has been configured"); return this.messagingTemplate.receive(channel); }
@Test public void testSelectParameterSourceFactoryInboundChannelAdapter() { setUp("pollingWithSelectParameterSourceJdbcInboundChannelAdapterTest.xml", getClass()); this.jdbcTemplate.update("insert into item values(1,'',42)"); Message<?> message = messagingTemplate.receive(); assertNotNull(message); assertEquals(42, ((Map<?, ?>) ((List<?>) message.getPayload()).get(0)).get("STATUS")); this.jdbcTemplate.update("insert into item values(2,'',84)"); this.appCtx.getBean(Status.class).which = 84; message = messagingTemplate.receive(); assertNotNull(message); assertEquals(84, ((Map<?, ?>) ((List<?>) message.getPayload()).get(0)).get("STATUS")); }
@Test public void testSimpleInboundChannelAdapterWithUpdate() { setUp("pollingForMapJdbcInboundChannelAdapterWithUpdateTest.xml", getClass()); this.jdbcTemplate.update("insert into item values(1,'',2)"); Message<?> message = messagingTemplate.receive(); assertNotNull(message); messagingTemplate.setReceiveTimeout(1); message = messagingTemplate.receive(); assertNull(message); }
@Test public void testSimpleInboundChannelAdapterWithNestedUpdate() { setUp("pollingForMapJdbcInboundChannelAdapterWithNestedUpdateTest.xml", getClass()); this.jdbcTemplate.update("insert into item values(1,'',2)"); Message<?> message = messagingTemplate.receive(); assertNotNull(message); messagingTemplate.setReceiveTimeout(1); message = messagingTemplate.receive(); assertNull(message); }
@Test public void testExtendedInboundChannelAdapter() { setUp("pollingWithJdbcOperationsJdbcInboundChannelAdapterTest.xml", getClass()); this.jdbcTemplate.update("insert into item values(1,'',2)"); Message<?> message = messagingTemplate.receive(); assertNotNull(message); }
@Test public void testParameterSourceInboundChannelAdapter() { setUp("pollingWithParametersForMapJdbcInboundChannelAdapterTest.xml", getClass()); this.jdbcTemplate.update("insert into item values(1,'',2)"); Message<?> message = messagingTemplate.receive(); assertNotNull(message); }
@Test public void testMaxRowsInboundChannelAdapter() { setUp("pollingWithMaxRowsJdbcInboundChannelAdapterTest.xml", getClass()); new TransactionTemplate(transactionManager).execute(status -> { jdbcTemplate.update("insert into item values(1,'',2)"); jdbcTemplate.update("insert into item values(2,'',2)"); jdbcTemplate.update("insert into item values(3,'',2)"); jdbcTemplate.update("insert into item values(4,'',2)"); return null; }); int count = 0; while (count < 4) { @SuppressWarnings("unchecked") Message<List<?>> message = (Message<List<?>>) messagingTemplate.receive(); assertNotNull(message); int payloadSize = message.getPayload().size(); assertTrue(payloadSize <= 2); count += payloadSize; } }
@Test public void testWithSelectQueryOnly() throws Exception { setUp("JdbcOutboundGatewayWithSelectTest-context.xml", getClass()); Message<?> message = MessageBuilder.withPayload(100).build(); this.channel.send(message); @SuppressWarnings("unchecked") Message<Map<String, Object>> reply = (Message<Map<String, Object>>) this.messagingTemplate.receive(); String id = (String) reply.getPayload().get("id"); Integer status = (Integer) reply.getPayload().get("status"); String name = (String) reply.getPayload().get("name"); assertEquals("100", id); assertEquals(Integer.valueOf(3), status); assertEquals("Cartman", name); }
@Test public void testCountUpdates() { setUp("handlingCountUpdatesJdbcOutboundGatewayTest.xml", getClass()); Message<?> message = MessageBuilder.withPayload(Collections.singletonMap("foo", "bar")).build(); this.channel.send(message); Message<?> reply = this.messagingTemplate.receive(); assertNotNull(reply); @SuppressWarnings("unchecked") Map<String, ?> payload = (Map<String, ?>) reply.getPayload(); assertEquals(1, payload.get("updated")); }
@Test public void testParameterSourceFactoryInboundChannelAdapter() { setUp("pollingWithParameterSourceJdbcInboundChannelAdapterTest.xml", getClass()); this.jdbcTemplate.update("insert into item values(1,'',2)"); Message<?> message = messagingTemplate.receive(); assertNotNull(message); List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT * FROM item WHERE status=1"); assertEquals(1, list.size()); assertEquals("BAR", list.get(0).get("NAME")); }
@Test public void testNoAutoStartupInboundChannelAdapter() { setUp("pollingNoAutoStartupJdbcInboundChannelAdapterTest.xml", getClass()); this.jdbcTemplate.update("insert into item values(1,'',2)"); messagingTemplate.setReceiveTimeout(1); Message<?> message = messagingTemplate.receive(); assertNull("Message found ", message); }
@Test public void testWithPoller() throws Exception { setUp("JdbcOutboundGatewayWithPollerTest-context.xml", this.getClass()); Message<?> message = MessageBuilder.withPayload(Collections.singletonMap("foo", "bar")).build(); this.channel.send(message); Message<?> reply = this.messagingTemplate.receive(); assertNotNull(reply); @SuppressWarnings("unchecked") Map<String, ?> payload = (Map<String, ?>) reply.getPayload(); assertEquals("bar", payload.get("name")); Map<String, Object> map = this.jdbcTemplate.queryForMap("SELECT * from BAZZ"); assertEquals("Wrong id", message.getHeaders().getId().toString(), map.get("ID")); assertEquals("Wrong name", "bar", map.get("name")); }
@Test @SuppressWarnings("unchecked") public void testKeyGeneration() { setUp("handlingKeyGenerationJdbcOutboundGatewayTest.xml", getClass()); Message<?> message = MessageBuilder.withPayload(Collections.singletonMap("foo", "bar")).build(); this.channel.send(message); Message<?> reply = this.messagingTemplate.receive(); assertNotNull(reply); Map<String, ?> payload = (Map<String, ?>) reply.getPayload(); Object id = payload.get("ID"); assertNotNull(id); Map<String, Object> map = this.jdbcTemplate.queryForMap("SELECT * from BARS"); assertEquals("Wrong id", id, map.get("ID")); assertEquals("Wrong name", "bar", map.get("name")); this.jdbcTemplate.execute("DELETE FROM BARS"); MessageChannel setterRequest = this.context.getBean("setterRequest", MessageChannel.class); setterRequest.send(new GenericMessage<String>("bar2")); reply = this.messagingTemplate.receive(); assertNotNull(reply); payload = (Map<String, ?>) reply.getPayload(); id = payload.get("ID"); assertNotNull(id); map = this.jdbcTemplate.queryForMap("SELECT * from BARS"); assertEquals("Wrong id", id, map.get("ID")); assertEquals("Wrong name", "bar2", map.get("name")); }
@Test public void testSimpleInboundChannelAdapter() { setUp("pollingForMapJdbcInboundChannelAdapterTest.xml", getClass()); this.jdbcTemplate.update("insert into item values(1,'',2)"); Message<?> message = messagingTemplate.receive(); assertNotNull("No message found ", message); assertTrue("Wrong payload type expected instance of List", message.getPayload() instanceof List<?>); MessageHistory history = MessageHistory.read(message); assertNotNull(history); Properties componentHistoryRecord = TestUtils.locateComponentInHistory(history, "jdbcAdapter", 0); assertNotNull(componentHistoryRecord); assertEquals("jdbc:inbound-channel-adapter", componentHistoryRecord.get("type")); }
@Test public void testMapPayloadMapReply() { setUp("handlingMapPayloadJdbcOutboundGatewayTest.xml", getClass()); assertTrue(this.context.containsBean("jdbcGateway")); Message<?> message = MessageBuilder.withPayload(Collections.singletonMap("foo", "bar")).build(); this.channel.send(message); Message<?> reply = this.messagingTemplate.receive(); assertNotNull(reply); @SuppressWarnings("unchecked") Map<String, ?> payload = (Map<String, ?>) reply.getPayload(); assertEquals("bar", payload.get("name")); Map<String, Object> map = this.jdbcTemplate.queryForMap("SELECT * from FOOS"); assertEquals("Wrong id", message.getHeaders().getId().toString(), map.get("ID")); assertEquals("Wrong name", "bar", map.get("name")); JdbcOutboundGateway gateway = context.getBean("jdbcGateway.handler", JdbcOutboundGateway.class); assertEquals(23, TestUtils.getPropertyValue(gateway, "order")); Assert.assertTrue(TestUtils.getPropertyValue(gateway, "requiresReply", Boolean.class)); assertEquals(1, adviceCalled); }