Search in sources :

Example 16 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class JdbcOutboundGatewayParserTests method testDefaultMaxMessagesPerPollIsSet.

@Test
public void testDefaultMaxMessagesPerPollIsSet() {
    setUp("JdbcOutboundGatewayWithPollerTest-context.xml", this.getClass());
    PollingConsumer pollingConsumer = this.context.getBean(PollingConsumer.class);
    DirectFieldAccessor accessor = new DirectFieldAccessor(pollingConsumer);
    Object source = accessor.getPropertyValue("handler");
    accessor = new DirectFieldAccessor(source);
    // JdbcPollingChannelAdapter
    source = accessor.getPropertyValue("poller");
    accessor = new DirectFieldAccessor(source);
    Integer maxRowsPerPoll = (Integer) accessor.getPropertyValue("maxRowsPerPoll");
    assertEquals("maxRowsPerPoll should default to 1", Integer.valueOf(1), maxRowsPerPoll);
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Test(org.junit.Test)

Example 17 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class ChatMessageOutboundChannelAdapterParserTests method withHeaderMapper.

@SuppressWarnings("rawtypes")
@Test
public void withHeaderMapper() throws Exception {
    Object pollingConsumer = context.getBean("withHeaderMapper");
    assertTrue(pollingConsumer instanceof PollingConsumer);
    assertEquals(headerMapper, TestUtils.getPropertyValue(pollingConsumer, "handler.headerMapper"));
    MessageChannel channel = context.getBean("outboundEventChannel", MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("hello").setHeader(XmppHeaders.TO, "oleg").setHeader("foobar", "foobar").build();
    XMPPConnection connection = context.getBean("testConnection", XMPPConnection.class);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        org.jivesoftware.smack.packet.Message xmppMessage = (org.jivesoftware.smack.packet.Message) args[0];
        assertEquals("oleg", xmppMessage.getTo().toString());
        assertEquals("foobar", JivePropertiesManager.getProperty(xmppMessage, "foobar"));
        return null;
    }).when(connection).sendStanza(Mockito.any(org.jivesoftware.smack.packet.Message.class));
    channel.send(message);
    verify(connection, times(1)).sendStanza(Mockito.any(org.jivesoftware.smack.packet.Message.class));
    Mockito.reset(connection);
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Test(org.junit.Test)

Example 18 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class ChatMessageOutboundChannelAdapterParserTests method testPollingConsumer.

@Test
public void testPollingConsumer() {
    Object pollingConsumer = context.getBean("withHeaderMapper");
    QueueChannel channel = (QueueChannel) TestUtils.getPropertyValue(pollingConsumer, "inputChannel");
    assertEquals("outboundPollingChannel", channel.getComponentName());
    assertTrue(pollingConsumer instanceof PollingConsumer);
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) Test(org.junit.Test)

Example 19 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class PresenceOutboundChannelAdapterParserTests method testRosterEventOutboundChannelAdapterParserAsPollingConsumer.

@Test
public void testRosterEventOutboundChannelAdapterParserAsPollingConsumer() {
    Object pollingConsumer = context.getBean("pollingOutboundRosterAdapter");
    assertTrue(pollingConsumer instanceof PollingConsumer);
    AbstractXmppConnectionAwareMessageHandler handler = (AbstractXmppConnectionAwareMessageHandler) TestUtils.getPropertyValue(pollingConsumer, "handler");
    assertEquals(23, TestUtils.getPropertyValue(handler, "order"));
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) AbstractXmppConnectionAwareMessageHandler(org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareMessageHandler) Test(org.junit.Test)

Example 20 with PollingConsumer

use of org.springframework.integration.endpoint.PollingConsumer in project spring-integration by spring-projects.

the class ContentEnricherTests method replyChannelReplyTimingOut.

/**
 * In this test a {@link Target} message is passed into a {@link ContentEnricher}.
 * The Enricher passes the message to a "request-channel" that is backed by a
 * {@link QueueChannel}. The consumer of the "request-channel" takes a long
 * time to execute, longer actually than the specified "replyTimeout" set on
 * the {@link ContentEnricher}.
 *
 * Due to the occurring replyTimeout, a Null replyMessage is returned and because
 * "requiresReply" is set to "true" on the {@link ContentEnricher}, a
 * {@link ReplyRequiredException} is raised.
 */
@Test
public void replyChannelReplyTimingOut() throws Exception {
    final long requestTimeout = 500L;
    final long replyTimeout = 100L;
    final DirectChannel replyChannel = new DirectChannel();
    final QueueChannel requestChannel = new QueueChannel(1);
    final ContentEnricher enricher = new ContentEnricher();
    enricher.setRequestChannel(requestChannel);
    enricher.setReplyChannel(replyChannel);
    enricher.setOutputChannel(new NullChannel());
    enricher.setRequestTimeout(requestTimeout);
    enricher.setReplyTimeout(replyTimeout);
    final ExpressionFactoryBean expressionFactoryBean = new ExpressionFactoryBean("payload");
    expressionFactoryBean.setSingleton(false);
    expressionFactoryBean.afterPropertiesSet();
    final Map<String, Expression> expressions = new HashMap<String, Expression>();
    expressions.put("name", new LiteralExpression("cartman"));
    expressions.put("child.name", expressionFactoryBean.getObject());
    enricher.setPropertyExpressions(expressions);
    enricher.setRequiresReply(true);
    enricher.setBeanName("Enricher");
    enricher.setBeanFactory(mock(BeanFactory.class));
    enricher.afterPropertiesSet();
    final AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        protected Object handleRequestMessage(Message<?> requestMessage) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                fail(e.getMessage());
            }
            return new Target("child");
        }
    };
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    final PollingConsumer consumer = new PollingConsumer(requestChannel, handler);
    final TestErrorHandler errorHandler = new TestErrorHandler();
    consumer.setTrigger(new PeriodicTrigger(0));
    consumer.setErrorHandler(errorHandler);
    consumer.setTaskScheduler(taskScheduler);
    consumer.setBeanFactory(mock(BeanFactory.class));
    consumer.afterPropertiesSet();
    consumer.start();
    final Target target = new Target("replace me");
    Message<?> requestMessage = MessageBuilder.withPayload(target).setReplyChannel(replyChannel).build();
    try {
        enricher.handleMessage(requestMessage);
    } catch (ReplyRequiredException e) {
        assertEquals("No reply produced by handler 'Enricher', and its 'requiresReply' property is set to true.", e.getMessage());
        return;
    }
    fail("ReplyRequiredException expected.");
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ReplyRequiredException(org.springframework.integration.handler.ReplyRequiredException) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) HashMap(java.util.HashMap) TestErrorHandler(org.springframework.integration.config.TestErrorHandler) LiteralExpression(org.springframework.expression.common.LiteralExpression) Matchers.containsString(org.hamcrest.Matchers.containsString) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) ExpressionFactoryBean(org.springframework.integration.config.ExpressionFactoryBean) LiteralExpression(org.springframework.expression.common.LiteralExpression) Expression(org.springframework.expression.Expression) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) NullChannel(org.springframework.integration.channel.NullChannel) Test(org.junit.Test)

Aggregations

PollingConsumer (org.springframework.integration.endpoint.PollingConsumer)25 Test (org.junit.Test)17 QueueChannel (org.springframework.integration.channel.QueueChannel)12 Message (org.springframework.messaging.Message)9 BeanFactory (org.springframework.beans.factory.BeanFactory)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)6 MessageChannel (org.springframework.messaging.MessageChannel)6 PollableChannel (org.springframework.messaging.PollableChannel)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)5 ErrorMessage (org.springframework.messaging.support.ErrorMessage)5 AbstractEndpoint (org.springframework.integration.endpoint.AbstractEndpoint)4 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)4 TestApplicationContext (org.springframework.integration.test.util.TestUtils.TestApplicationContext)4 SubscribableChannel (org.springframework.messaging.SubscribableChannel)4 ReactiveStreamsConsumer (org.springframework.integration.endpoint.ReactiveStreamsConsumer)3 MessagingException (org.springframework.messaging.MessagingException)3 PeriodicTrigger (org.springframework.scheduling.support.PeriodicTrigger)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2