protected PublishSubscribeChannelSpec(Executor executor) { this.channel = new PublishSubscribeChannel(executor); }
@Bean public SubscribableChannel scatterAuctionWithoutGatherChannel() { PublishSubscribeChannel channel = new PublishSubscribeChannel(); channel.setApplySequence(true); return channel; }
public S ignoreFailures(boolean ignoreFailures) { this.channel.setIgnoreFailures(ignoreFailures); return _this(); }
super.onInit(); if (this.executor != null) { Assert.state(getDispatcher().getHandlerCount() == 0, "When providing an Executor, you cannot subscribe() until the channel " + "bean is fully initialized by the framework. Do not subscribe in a @Bean definition"); if (this.errorHandler == null) { this.errorHandler = new MessagePublishingErrorHandler( new BeanFactoryChannelResolver(this.getBeanFactory())); getDispatcher().setIgnoreFailures(this.ignoreFailures); getDispatcher().setApplySequence(this.applySequence); getDispatcher().setMinSubscribers(this.minSubscribers); this.logger.warn("The 'errorHandler' is ignored for the '" + getComponentName() + "' (an 'executor' is not provided) and exceptions will be thrown " + "directly within the sending Thread"); getIntegrationProperty(IntegrationProperties.CHANNELS_MAX_BROADCAST_SUBSCRIBERS, Integer.class); this.setMaxSubscribers(maxSubscribers); getDispatcher().setBeanFactory(this.getBeanFactory()); getDispatcher().setMessageHandlingTaskDecorator(task -> { if (PublishSubscribeChannel.this.executorInterceptorsSize > 0) { return new MessageHandlingTask(task);
@Test public void testEarlySubscribe() { PublishSubscribeChannel channel = new PublishSubscribeChannel(mock(Executor.class)); try { channel.subscribe(m -> { }); channel.setBeanFactory(mock(BeanFactory.class)); channel.afterPropertiesSet(); fail("expected Exception"); } catch (IllegalStateException e) { assertThat(e.getMessage(), equalTo("When providing an Executor, you cannot subscribe() until the channel " + "bean is fully initialized by the framework. Do not subscribe in a @Bean definition")); } }
@Test(expected = MessageDeliveryException.class) public void validateExceptionIfSendToErrorChannelFails() { DirectChannel outChannel = new DirectChannel(); outChannel.subscribe(message -> { throw new RuntimeException("problems"); }); PublishSubscribeChannel errorChannel = new PublishSubscribeChannel(); errorChannel.subscribe(message -> { throw new RuntimeException("ooops"); }); MessageProducerSupport mps = new MessageProducerSupport() { }; mps.setOutputChannel(outChannel); mps.setErrorChannel(errorChannel); mps.setBeanFactory(this.context); mps.afterPropertiesSet(); mps.start(); mps.sendMessage(new GenericMessage<>("hello")); }
@Test(expected = MessageDeliveryException.class) public void handlerThrowsExceptionPublishSubscribeWithoutExecutor() { PublishSubscribeChannel channel = new PublishSubscribeChannel(); channel.subscribe(message -> { throw new UnsupportedOperationException("intentional test failure"); }); Message<?> message = MessageBuilder.withPayload("test").build(); channel.send(message); }
@Test public void testPubSubChannelLoggingWithMoreThenOneSubscriber() { final PublishSubscribeChannel channel = new PublishSubscribeChannel(); channel.setBeanName("pubSubChannel"); final Log logger = mock(Log.class); when(logger.isInfoEnabled()).thenReturn(true); ReflectionUtils.doWithFields(AbstractMessageChannel.class, field -> { if ("logger".equals(field.getName())) { field.setAccessible(true); field.set(channel, logger); } }); channel.subscribe(mock(MessageHandler.class)); channel.subscribe(mock(MessageHandler.class)); verify(logger, times(2)).info(Mockito.anyString()); } }
/** * Represent an Integration Flow as a Reactive Streams {@link Publisher} bean. * @param executor the managed {@link Executor} to be used for the background task to * poll messages from the {@link PollableChannel}. * Defaults to {@link Executors#newSingleThreadExecutor()}. * @param <T> the {@code payload} type * @return the Reactive Streams {@link Publisher} */ public <T> Publisher<Message<T>> toReactivePublisher(Executor executor) { Assert.notNull(executor, "'executor' must not be null"); MessageChannel channelForPublisher = this.currentMessageChannel; if (channelForPublisher == null) { PublishSubscribeChannel publishSubscribeChannel = new PublishSubscribeChannel(); publishSubscribeChannel.setMinSubscribers(1); channelForPublisher = publishSubscribeChannel; channel(channelForPublisher); } get(); return new PublisherIntegrationFlow<T>(this.integrationComponents, channelForPublisher, executor); }
@Override protected PublishSubscribeChannel createSharedChannel(String name) { PublishSubscribeChannel publishSubscribeChannel = new PublishSubscribeChannel(executor); publishSubscribeChannel.setIgnoreFailures(true); return publishSubscribeChannel; } };
@Test public void bothConsumersReceivePublishSubscribeMessage() throws InterruptedException { TestApplicationContext context = TestUtils.createTestApplicationContext(); PublishSubscribeChannel inputChannel = new PublishSubscribeChannel(); QueueChannel outputChannel1 = new QueueChannel(); QueueChannel outputChannel2 = new QueueChannel(); context.registerEndpoint("testEndpoint2", endpoint2); context.refresh(); inputChannel.send(new GenericMessage<String>("testing")); latch.await(500, TimeUnit.MILLISECONDS); assertEquals("both handlers should have been invoked", 0, latch.getCount());
PublishSubscribeChannel channel = new PublishSubscribeChannel(); channel.addInterceptor(new ChannelInterceptorAdapter() {
public S applySequence(boolean applySequence) { this.channel.setApplySequence(applySequence); return _this(); }
login("bob", "bobspassword", "ROLE_ADMIN", "ROLE_PRESIDENT"); this.publishSubscribeChannel.send(new GenericMessage<String>("test")); assertEquals(0, headerAccessor.getSequenceNumber()); this.publishSubscribeChannel.setApplySequence(true); this.publishSubscribeChannel.send(new GenericMessage<String>("test")); assertEquals(2, headerAccessor.getSequenceNumber()); this.publishSubscribeChannel.setApplySequence(false); this.publishSubscribeChannel.send(new GenericMessage<String>("test")); Message<?> errorMessage = this.errorChannel.receive(10000); assertNotNull(errorMessage);
@Test public void testProxyPubSubWithExec() throws Exception { assertTrue(AopUtils.isCglibProxy(this.publishSubscribeChannel)); final AtomicReference<Message<?>> message = new AtomicReference<>(); final CountDownLatch latch = new CountDownLatch(1); this.publishSubscribeChannel.subscribe(m -> { message.set(m); latch.countDown(); }); this.publishSubscribeChannel.send(new GenericMessage<>("foo")); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertNotNull(message.get()); }
public S maxSubscribers(Integer maxSubscribers) { this.channel.setMaxSubscribers(maxSubscribers); return _this(); }
public S minSubscribers(int minSubscribers) { this.channel.setMinSubscribers(minSubscribers); return _this(); }
public S errorHandler(ErrorHandler errorHandler) { this.channel.setErrorHandler(errorHandler); return _this(); }
@Override public boolean subscribe(MessageHandler handler) { this.subscribers.incrementAndGet(); if (handler instanceof LastSubscriberMessageHandler && this.finalHandler != null) { throw new IllegalStateException("Only one LastSubscriberMessageHandler is allowed"); } if (this.finalHandler != null) { unsubscribe(this.finalHandler); } boolean result = super.subscribe(handler); if (this.finalHandler != null) { super.subscribe(finalHandler); } if (handler instanceof LastSubscriberMessageHandler && this.finalHandler == null) { this.finalHandler = (LastSubscriberMessageHandler) handler; } return result; }
@Test public void validateErrorChannelWithSuccessfulReply() throws InterruptedException { DirectChannel reqChannel = new DirectChannel(); reqChannel.subscribe(message -> { throw new RuntimeException("ooops"); }); PublishSubscribeChannel errorChannel = new PublishSubscribeChannel(); MyOneWayErrorService myOneWayErrorService = new MyOneWayErrorService(); ServiceActivatingHandler handler = new ServiceActivatingHandler(myOneWayErrorService); handler.setBeanFactory(this.applicationContext); handler.afterPropertiesSet(); errorChannel.subscribe(handler); this.messagingGateway = new MessagingGatewaySupport() { }; this.messagingGateway.setRequestChannel(reqChannel); this.messagingGateway.setErrorChannel(errorChannel); this.messagingGateway.setReplyChannel(null); this.messagingGateway.setBeanFactory(mock(BeanFactory.class)); this.messagingGateway.afterPropertiesSet(); this.messagingGateway.start(); this.messagingGateway.send("hello"); assertTrue(myOneWayErrorService.errorReceived.await(10, TimeUnit.SECONDS)); }