Search in sources :

Example 1 with NullChannel

use of org.springframework.integration.channel.NullChannel in project spring-integration by spring-projects.

the class OutboundEndpointTests method testDelayExpression.

@Test
public void testDelayExpression() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    RabbitTemplate amqpTemplate = spy(new RabbitTemplate(connectionFactory));
    AmqpOutboundEndpoint endpoint = new AmqpOutboundEndpoint(amqpTemplate);
    willDoNothing().given(amqpTemplate).send(anyString(), anyString(), any(Message.class), isNull());
    willAnswer(invocation -> invocation.getArgument(2)).given(amqpTemplate).sendAndReceive(anyString(), anyString(), any(Message.class), isNull());
    endpoint.setExchangeName("foo");
    endpoint.setRoutingKey("bar");
    endpoint.setDelayExpressionString("42");
    endpoint.setBeanFactory(mock(BeanFactory.class));
    endpoint.afterPropertiesSet();
    endpoint.handleMessage(new GenericMessage<>("foo"));
    ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
    verify(amqpTemplate).send(eq("foo"), eq("bar"), captor.capture(), isNull());
    assertThat(captor.getValue().getMessageProperties().getDelay(), equalTo(42));
    endpoint.setExpectReply(true);
    endpoint.setOutputChannel(new NullChannel());
    endpoint.handleMessage(new GenericMessage<>("foo"));
    verify(amqpTemplate).sendAndReceive(eq("foo"), eq("bar"), captor.capture(), isNull());
    assertThat(captor.getValue().getMessageProperties().getDelay(), equalTo(42));
    endpoint.setDelay(23);
    endpoint.setRoutingKey("baz");
    endpoint.afterPropertiesSet();
    endpoint.handleMessage(new GenericMessage<>("foo"));
    verify(amqpTemplate).sendAndReceive(eq("foo"), eq("baz"), captor.capture(), isNull());
    assertThat(captor.getValue().getMessageProperties().getDelay(), equalTo(23));
}
Also used : AsyncRabbitTemplate(org.springframework.amqp.rabbit.AsyncRabbitTemplate) RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) NullChannel(org.springframework.integration.channel.NullChannel) Test(org.junit.Test)

Example 2 with NullChannel

use of org.springframework.integration.channel.NullChannel in project spring-integration by spring-projects.

the class AmqpOutboundChannelAdapterParserTests method parseWithPublisherConfirms.

@Test
public void parseWithPublisherConfirms() {
    Object eventDrivenConsumer = context.getBean("withPublisherConfirms");
    AmqpOutboundEndpoint endpoint = TestUtils.getPropertyValue(eventDrivenConsumer, "handler", AmqpOutboundEndpoint.class);
    NullChannel nullChannel = context.getBean(NullChannel.class);
    MessageChannel ackChannel = context.getBean("ackChannel", MessageChannel.class);
    assertSame(ackChannel, TestUtils.getPropertyValue(endpoint, "confirmAckChannel"));
    assertSame(nullChannel, TestUtils.getPropertyValue(endpoint, "confirmNackChannel"));
    assertSame(context.getBean("ems"), TestUtils.getPropertyValue(endpoint, "errorMessageStrategy"));
}
Also used : AmqpOutboundEndpoint(org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint) MessageChannel(org.springframework.messaging.MessageChannel) NullChannel(org.springframework.integration.channel.NullChannel) Test(org.junit.Test)

Example 3 with NullChannel

use of org.springframework.integration.channel.NullChannel in project spring-integration by spring-projects.

the class AbstractAmqpOutboundEndpoint method doInit.

@Override
protected final void doInit() {
    Assert.state(this.exchangeNameExpression == null || this.exchangeName == null, "Either an exchangeName or an exchangeNameExpression can be provided, but not both");
    BeanFactory beanFactory = getBeanFactory();
    if (this.exchangeNameExpression != null) {
        this.exchangeNameGenerator = new ExpressionEvaluatingMessageProcessor<String>(this.exchangeNameExpression, String.class);
        if (beanFactory != null) {
            this.exchangeNameGenerator.setBeanFactory(beanFactory);
        }
    }
    Assert.state(this.routingKeyExpression == null || this.routingKey == null, "Either a routingKey or a routingKeyExpression can be provided, but not both");
    if (this.routingKeyExpression != null) {
        this.routingKeyGenerator = new ExpressionEvaluatingMessageProcessor<String>(this.routingKeyExpression, String.class);
        if (beanFactory != null) {
            this.routingKeyGenerator.setBeanFactory(beanFactory);
        }
    }
    if (this.confirmCorrelationExpression != null) {
        this.correlationDataGenerator = new ExpressionEvaluatingMessageProcessor<Object>(this.confirmCorrelationExpression, Object.class);
        if (beanFactory != null) {
            this.correlationDataGenerator.setBeanFactory(beanFactory);
        }
    } else {
        NullChannel nullChannel = extractTypeIfPossible(this.confirmAckChannel, NullChannel.class);
        Assert.state((this.confirmAckChannel == null || nullChannel != null) && this.confirmAckChannelName == null, "A 'confirmCorrelationExpression' is required when specifying a 'confirmAckChannel'");
        nullChannel = extractTypeIfPossible(this.confirmNackChannel, NullChannel.class);
        Assert.state((this.confirmNackChannel == null || nullChannel != null) && this.confirmNackChannelName == null, "A 'confirmCorrelationExpression' is required when specifying a 'confirmNackChannel'");
    }
    if (this.delayExpression != null) {
        this.delayGenerator = new ExpressionEvaluatingMessageProcessor<Integer>(this.delayExpression, Integer.class);
        if (beanFactory != null) {
            this.delayGenerator.setBeanFactory(beanFactory);
        }
    }
    endpointInit();
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) NullChannel(org.springframework.integration.channel.NullChannel)

Example 4 with NullChannel

use of org.springframework.integration.channel.NullChannel in project spring-integration by spring-projects.

the class SourcePollingChannelAdapterFactoryBeanTests method testInterrupted.

@Test
public void testInterrupted() throws Exception {
    final CountDownLatch startLatch = new CountDownLatch(1);
    MessageSource<Object> ms = () -> {
        startLatch.countDown();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new MessagingException("Interrupted awaiting stopLatch", e);
        }
        return null;
    };
    SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
    taskScheduler.setAwaitTerminationSeconds(1);
    taskScheduler.afterPropertiesSet();
    pollingChannelAdapter.setTaskScheduler(taskScheduler);
    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    Log errorHandlerLogger = TestUtils.getPropertyValue(errorHandler, "logger", Log.class);
    errorHandlerLogger = spy(errorHandlerLogger);
    DirectFieldAccessor dfa = new DirectFieldAccessor(errorHandler);
    dfa.setPropertyValue("logger", errorHandlerLogger);
    pollingChannelAdapter.setErrorHandler(errorHandler);
    pollingChannelAdapter.setSource(ms);
    pollingChannelAdapter.setOutputChannel(new NullChannel());
    pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
    pollingChannelAdapter.afterPropertiesSet();
    Log adapterLogger = TestUtils.getPropertyValue(pollingChannelAdapter, "logger", Log.class);
    adapterLogger = spy(adapterLogger);
    when(adapterLogger.isDebugEnabled()).thenReturn(true);
    dfa = new DirectFieldAccessor(pollingChannelAdapter);
    dfa.setPropertyValue("logger", adapterLogger);
    pollingChannelAdapter.start();
    assertTrue(startLatch.await(10, TimeUnit.SECONDS));
    pollingChannelAdapter.stop();
    taskScheduler.shutdown();
    verifyZeroInteractions(errorHandlerLogger);
    verify(adapterLogger).debug(contains("Poll interrupted - during stop()?"));
}
Also used : MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) MessagingException(org.springframework.messaging.MessagingException) Log(org.apache.commons.logging.Log) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) NullChannel(org.springframework.integration.channel.NullChannel) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 5 with NullChannel

use of org.springframework.integration.channel.NullChannel in project spring-integration by spring-projects.

the class MessageMetricsAdviceTests method setUp.

@Before
public void setUp() throws Exception {
    this.applicationContext = TestUtils.createTestApplicationContext();
    this.beanFactory = this.applicationContext.getBeanFactory();
    this.channel = new NullChannel();
    this.mBeanExporter = new IntegrationMBeanExporter();
    this.mBeanExporter.setApplicationContext(this.applicationContext);
    this.mBeanExporter.setBeanFactory(this.beanFactory);
    this.mBeanExporter.setBeanClassLoader(ClassUtils.getDefaultClassLoader());
    this.mBeanExporter.afterPropertiesSet();
    this.handler = new DummyHandler();
    applicationContext.refresh();
}
Also used : NullChannel(org.springframework.integration.channel.NullChannel) Before(org.junit.Before)

Aggregations

NullChannel (org.springframework.integration.channel.NullChannel)19 Test (org.junit.Test)12 BeanFactory (org.springframework.beans.factory.BeanFactory)10 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)7 File (java.io.File)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Log (org.apache.commons.logging.Log)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Message (org.springframework.amqp.core.Message)2 AsyncRabbitTemplate (org.springframework.amqp.rabbit.AsyncRabbitTemplate)2 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)2 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)2 TaskScheduler (org.springframework.scheduling.TaskScheduler)2 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1