Java Code Examples for org.springframework.messaging.SubscribableChannel#subscribe()
The following examples show how to use
org.springframework.messaging.SubscribableChannel#subscribe() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamListenerHandlerMethodTests.java From spring-cloud-stream with Apache License 2.0 | 6 votes |
@StreamListener public void receive(@Input(Processor.INPUT) SubscribableChannel input, @Output(Processor.OUTPUT) final MessageChannel output1, @Output(StreamListenerTestUtils.FooOutboundChannel1.OUTPUT) final MessageChannel output2) { input.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { if (message.getHeaders().get("output").equals("output1")) { output1.send(org.springframework.messaging.support.MessageBuilder .withPayload( message.getPayload().toString().toUpperCase()) .build()); } else if (message.getHeaders().get("output").equals("output2")) { output2.send(org.springframework.messaging.support.MessageBuilder .withPayload( message.getPayload().toString().toLowerCase()) .build()); } } }); }
Example 2
Source File: TestConsumer.java From spring-cloud-consul with Apache License 2.0 | 6 votes |
@Override public void run(ApplicationArguments args) throws Exception { logger.info("Consumer running with binder {}", this.binder); SubscribableChannel consumerChannel = new ExecutorSubscribableChannel(); consumerChannel.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { TestConsumer.this.messagePayload = (String) message.getPayload(); logger.info("Received message: {}", TestConsumer.this.messagePayload); } }); String group = null; if (args.containsOption("group")) { group = args.getOptionValues("group").get(0); } this.binder.bindConsumer(ConsulBinderTests.BINDING_NAME, group, consumerChannel, new ConsumerProperties()); this.isBound = true; }
Example 3
Source File: OrderApplication.java From sca-best-practice with Apache License 2.0 | 5 votes |
public static void main(String[] args) { ApplicationContext applicationContext = SpringApplication.run(OrderApplication.class, args); SubscribableChannel input = (SubscribableChannel)applicationContext.getBean("input"); input.subscribe(message -> { try { System.out.println("Get mq message ,content is : " + new String((byte[])message.getPayload(), "UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }); }
Example 4
Source File: GenericMessagingTemplateTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void sendAndReceive() { SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor); channel.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel(); replyChannel.send(new GenericMessage<>("response")); } }); String actual = this.template.convertSendAndReceive(channel, "request", String.class); assertEquals("response", actual); }
Example 5
Source File: GenericMessagingTemplateTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void sendAndReceive() { SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor); channel.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel(); replyChannel.send(new GenericMessage<>("response")); } }); String actual = this.template.convertSendAndReceive(channel, "request", String.class); assertEquals("response", actual); }
Example 6
Source File: GenericMessagingTemplateTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void sendAndReceive() { SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor); channel.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel(); replyChannel.send(new GenericMessage<String>("response")); } }); String actual = this.template.convertSendAndReceive(channel, "request", String.class); assertEquals("response", actual); }
Example 7
Source File: PollableConsumerTests.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@Test public void testRequeueFromErrorFlow() { TestChannelBinder binder = createBinder(); MessageConverterConfigurer configurer = this.context .getBean(MessageConverterConfigurer.class); DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource( this.messageConverter); configurer.configurePolledMessageSource(pollableSource, "foo"); AcknowledgmentCallback callback = mock(AcknowledgmentCallback.class); pollableSource.addInterceptor(new ChannelInterceptor() { @Override public Message<?> preSend(Message<?> message, MessageChannel channel) { return MessageBuilder.fromMessage(message) .setHeader( IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback) .build(); } }); ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null); properties.setMaxAttempts(1); binder.bindPollableConsumer("foo", "bar", pollableSource, properties); SubscribableChannel errorChannel = new DirectChannel(); errorChannel.subscribe(msg -> { throw new RequeueCurrentMessageException((Throwable) msg.getPayload()); }); pollableSource.setErrorChannel(errorChannel); try { pollableSource.poll(received -> { throw new RuntimeException("test requeue from error flow"); }); } catch (Exception e) { // no op } verify(callback).acknowledge(Status.REQUEUE); }
Example 8
Source File: TestChannelBinder.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@Override protected MessageProducer createConsumerEndpoint(ConsumerDestination destination, String group, ConsumerProperties properties) throws Exception { ErrorMessageStrategy errorMessageStrategy = new DefaultErrorMessageStrategy(); SubscribableChannel siBinderInputChannel = ((SpringIntegrationConsumerDestination) destination) .getChannel(); IntegrationMessageListeningContainer messageListenerContainer = new IntegrationMessageListeningContainer(); IntegrationBinderInboundChannelAdapter adapter = new IntegrationBinderInboundChannelAdapter( messageListenerContainer); String groupName = StringUtils.hasText(group) ? group : "anonymous"; ErrorInfrastructure errorInfrastructure = registerErrorInfrastructure(destination, groupName, properties); if (properties.getMaxAttempts() > 1) { adapter.setRetryTemplate(buildRetryTemplate(properties)); adapter.setRecoveryCallback(errorInfrastructure.getRecoverer()); } else { adapter.setErrorMessageStrategy(errorMessageStrategy); adapter.setErrorChannel(errorInfrastructure.getErrorChannel()); } siBinderInputChannel.subscribe(messageListenerContainer); return adapter; }
Example 9
Source File: StreamListenerHandlerMethodTests.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@StreamListener @Output(StreamListenerTestUtils.FooOutboundChannel1.OUTPUT) public void receive(@Input(Processor.INPUT) SubscribableChannel input, @Output(Processor.OUTPUT) final MessageChannel output1) { input.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { output1.send(org.springframework.messaging.support.MessageBuilder .withPayload(message.getPayload().toString().toUpperCase()) .build()); } }); }
Example 10
Source File: StreamListenerWithAnnotatedInputOutputArgsTests.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@StreamListener public void receive(@Input(Processor.INPUT) SubscribableChannel input, @Output(Processor.OUTPUT) final MessageChannel output) { input.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { output.send(MessageBuilder .withPayload(message.getPayload().toString().toUpperCase()) .build()); } }); }
Example 11
Source File: StreamListenerWithAnnotatedInputOutputArgsTests.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@StreamListener public void receive(@Input(Processor.INPUT) SubscribableChannel input, @Output(Processor.OUTPUT) final MessageChannel output, String someArg) { input.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { output.send(MessageBuilder .withPayload(message.getPayload().toString().toUpperCase()) .build()); } }); }
Example 12
Source File: StreamListenerWithAnnotatedInputOutputArgsTests.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@StreamListener public void receive(@Input("invalid") SubscribableChannel input, @Output(Processor.OUTPUT) final MessageChannel output) { input.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { output.send(MessageBuilder .withPayload(message.getPayload().toString().toUpperCase()) .build()); } }); }
Example 13
Source File: StreamListenerWithAnnotatedInputOutputArgsTests.java From spring-cloud-stream with Apache License 2.0 | 5 votes |
@StreamListener public void receive(@Output(Processor.OUTPUT) final MessageChannel output, @Input("input") SubscribableChannel input) { input.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { output.send(MessageBuilder .withPayload(message.getPayload().toString().toUpperCase()) .build()); } }); }
Example 14
Source File: KinesisBinderTests.java From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 | 4 votes |
@Test @SuppressWarnings("unchecked") public void testProducerErrorChannel() throws Exception { KinesisTestBinder binder = getBinder(); final RuntimeException putRecordException = new RuntimeException( "putRecordRequestEx"); final AtomicReference<Object> sent = new AtomicReference<>(); AmazonKinesisAsync amazonKinesisMock = mock(AmazonKinesisAsync.class); BDDMockito .given(amazonKinesisMock.putRecordAsync(any(PutRecordRequest.class), any(AsyncHandler.class))) .willAnswer((Answer<Future<PutRecordResult>>) (invocation) -> { PutRecordRequest request = invocation.getArgument(0); sent.set(request.getData()); AsyncHandler<?, ?> handler = invocation.getArgument(1); handler.onError(putRecordException); return mock(Future.class); }); new DirectFieldAccessor(binder.getBinder()).setPropertyValue("amazonKinesis", amazonKinesisMock); ExtendedProducerProperties<KinesisProducerProperties> producerProps = createProducerProperties(); producerProps.setErrorChannelEnabled(true); DirectChannel moduleOutputChannel = createBindableChannel("output", createProducerBindingProperties(producerProps)); Binding<MessageChannel> producerBinding = binder.bindProducer("ec.0", moduleOutputChannel, producerProps); ApplicationContext applicationContext = TestUtils.getPropertyValue( binder.getBinder(), "applicationContext", ApplicationContext.class); SubscribableChannel ec = applicationContext.getBean("ec.0.errors", SubscribableChannel.class); final AtomicReference<Message<?>> errorMessage = new AtomicReference<>(); final CountDownLatch latch = new CountDownLatch(1); ec.subscribe((message) -> { errorMessage.set(message); latch.countDown(); }); String messagePayload = "oops"; moduleOutputChannel.send(new GenericMessage<>(messagePayload.getBytes())); assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue(); assertThat(errorMessage.get()).isInstanceOf(ErrorMessage.class); assertThat(errorMessage.get().getPayload()) .isInstanceOf(AwsRequestFailureException.class); AwsRequestFailureException exception = (AwsRequestFailureException) errorMessage .get().getPayload(); assertThat(exception.getCause()).isSameAs(putRecordException); assertThat(((PutRecordRequest) exception.getRequest()).getData()) .isSameAs(sent.get()); producerBinding.unbind(); }
Example 15
Source File: GenericMessagingTemplateTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
@Test public void sendAndReceiveTimeout() throws InterruptedException { final AtomicReference<Throwable> failure = new AtomicReference<Throwable>(); final CountDownLatch latch = new CountDownLatch(1); this.template.setReceiveTimeout(1); this.template.setThrowExceptionOnLateReply(true); SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor); channel.subscribe(new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { try { Thread.sleep(500); MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel(); replyChannel.send(new GenericMessage<String>("response")); failure.set(new IllegalStateException("Expected exception")); } catch (InterruptedException e) { failure.set(e); } catch (MessageDeliveryException ex) { String expected = "Reply message received but the receiving thread has exited due to a timeout"; String actual = ex.getMessage(); if (!expected.equals(actual)) { failure.set(new IllegalStateException("Unexpected error: '" + actual + "'")); } } finally { latch.countDown(); } } }); assertNull(this.template.convertSendAndReceive(channel, "request", String.class)); assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); if (failure.get() != null) { throw new AssertionError(failure.get()); } }