/** * Receive from the {@link MessageSource} and send immediately to the input channel, so that the call that we are * intercepting always a message to receive. * * @see ChannelInterceptor#preReceive(MessageChannel) */ @Override public boolean preReceive(MessageChannel channel) { Message<?> message = source.receive(); if (message != null) { if (this.channel != null) { channel = this.channel; } channel.send(message); if (logger.isDebugEnabled()) { logger.debug("Sent " + message + " to channel " + channel); } return true; } return true; }
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(); }
public void write(List<? extends T> items) throws Exception { // Block until expecting <= throttle limit while (localState.getExpecting() > throttleLimit) { getNextResult(); } if (!items.isEmpty()) { ChunkRequest<T> request = localState.getRequest(items); if (logger.isDebugEnabled()) { logger.debug("Dispatching chunk: " + request); } messagingGateway.send(new GenericMessage<>(request)); localState.incrementExpected(); } }
@Before public void setUp() { jobRepository = new SimpleJobRepository(new MapJobInstanceDao(), new MapJobExecutionDao(), new MapStepExecutionDao(), new MapExecutionContextDao()); factory.setJobRepository(jobRepository); factory.setTransactionManager(new ResourcelessTransactionManager()); factory.setBeanName("step"); factory.setItemWriter(writer); factory.setCommitInterval(4); MessagingTemplate gateway = new MessagingTemplate(); writer.setMessagingOperations(gateway); gateway.setDefaultChannel(requests); writer.setReplyChannel(replies); gateway.setReceiveTimeout(100); TestItemWriter.count = 0; // Drain queues Message<?> message = replies.receive(10); while (message != null) { System.err.println(message); message = replies.receive(10); } }
private Object sendAndReceive(MessageChannel channel, Object payload) { MessagingTemplate template = new MessagingTemplate(); template.setDefaultDestination(channel); return template.convertSendAndReceive(payload, Object.class); }
/** * Build a master {@link TaskletStep}. * * @return the configured master step * @see RemoteChunkHandlerFactoryBean */ public TaskletStep build() { Assert.notNull(this.inputChannel, "An InputChannel must be provided"); Assert.state(this.outputChannel == null || this.messagingTemplate == null, "You must specify either an outputChannel or a messagingTemplate but not both."); // configure messaging template if (this.messagingTemplate == null) { this.messagingTemplate = new MessagingTemplate(); this.messagingTemplate.setDefaultChannel(this.outputChannel); if (this.logger.isDebugEnabled()) { this.logger.debug("No messagingTemplate was provided, using a default one"); } } // configure item writer ChunkMessageChannelItemWriter<O> chunkMessageChannelItemWriter = new ChunkMessageChannelItemWriter<>(); chunkMessageChannelItemWriter.setMessagingOperations(this.messagingTemplate); chunkMessageChannelItemWriter.setMaxWaitTimeouts(this.maxWaitTimeouts); chunkMessageChannelItemWriter.setThrottleLimit(this.throttleLimit); chunkMessageChannelItemWriter.setReplyChannel(this.inputChannel); super.writer(chunkMessageChannelItemWriter); return super.build(); }
@Test public void eitherOutputChannelOrMessagingTemplateMustBeProvided() { // given RemotePartitioningMasterStepBuilder builder = new RemotePartitioningMasterStepBuilder("step") .outputChannel(new DirectChannel()) .messagingTemplate(new MessagingTemplate()); this.expectedException.expect(IllegalStateException.class); this.expectedException.expectMessage("You must specify either an outputChannel or a messagingTemplate but not both."); // when Step step = builder.build(); // then // expected exception }
/** * Set the timeout for sending a message to the resolved channel. * By default, there is no timeout, meaning the send will block indefinitely. * @param timeout The timeout. * @since 4.3 */ public void setSendTimeout(long timeout) { this.messagingTemplate.setSendTimeout(timeout); }
/** * Publish an error message for the supplied message and throwable. If the throwable * is already a {@link MessagingException} containing the message in its * {@code failedMessage} property, use {@link #publish(MessagingException)} instead. * @param failedMessage the message. * @param throwable the throwable. */ public void publish(Message<?> failedMessage, Throwable throwable) { publish(null, failedMessage, throwable); }
/** * Sends {@link StepExecutionRequest} objects to the request channel of the {@link MessagingTemplate}, and then * receives the result back as a list of {@link StepExecution} on a reply channel. Use the {@link #aggregate(List)} * method as an aggregator of the individual remote replies. The receive timeout needs to be set realistically in * the {@link MessagingTemplate} <b>and</b> the aggregator, so that there is a good chance of all work being done. * * @see PartitionHandler#handle(StepExecutionSplitter, StepExecution) */ public Collection<StepExecution> handle(StepExecutionSplitter stepExecutionSplitter, final StepExecution masterStepExecution) throws Exception { final Set<StepExecution> split = stepExecutionSplitter.split(masterStepExecution, gridSize); if(CollectionUtils.isEmpty(split)) { return split; } int count = 0; for (StepExecution stepExecution : split) { Message<StepExecutionRequest> request = createMessage(count++, split.size(), new StepExecutionRequest( stepName, stepExecution.getJobExecutionId(), stepExecution.getId()), replyChannel); if (logger.isDebugEnabled()) { logger.debug("Sending request: " + request); } messagingGateway.send(request); } if(!pollRepositoryForResults) { return receiveReplies(replyChannel); } else { return pollReplies(masterStepExecution, split); } }
this.messagingTemplate = new MessagingTemplate(); this.messagingTemplate.setDefaultChannel(this.outputChannel); if (this.logger.isDebugEnabled()) { this.logger.debug("No messagingTemplate was provided, using a default one");
@Test public void eitherOutputChannelOrMessagingTemplateMustBeProvided() { // given RemoteChunkingMasterStepBuilder<String, String> builder = new RemoteChunkingMasterStepBuilder<String, String>("step") .inputChannel(this.inputChannel) .outputChannel(new DirectChannel()) .messagingTemplate(new MessagingTemplate()); this.expectedException.expect(IllegalStateException.class); this.expectedException.expectMessage("You must specify either an outputChannel or a messagingTemplate but not both."); // when TaskletStep step = builder.build(); // then // expected exception }
Message<ChunkResponse> message = (Message<ChunkResponse>) messagingGateway.receive(replyChannel); if (message != null) { ChunkResponse payload = message.getPayload();
/** * Set the timeout for sending reply Messages. * @param sendTimeout The send timeout. */ public void setSendTimeout(long sendTimeout) { this.messagingTemplate.setSendTimeout(sendTimeout); }
verify(operations, times(3)).send(any(Message.class));
@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()); }
/** * Configure the default timeout value to use for send operations. * May be overridden for individual messages. * @param sendTimeout the send timeout in milliseconds * @see MessagingTemplate#setSendTimeout */ public void setSendTimeout(long sendTimeout) { this.messagingTemplate.setSendTimeout(sendTimeout); }
@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()); }
/** * Specify the timeout value for sending to the discard channel. * @param timeout the timeout in milliseconds */ public void setTimeout(long timeout) { this.messagingTemplate.setSendTimeout(timeout); }