Search in sources :

Example 71 with DirectChannel

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

the class JdbcMessageStoreTests method testWithMessageHistory.

@Test
public void testWithMessageHistory() throws Exception {
    Message<?> message = new GenericMessage<String>("Hello");
    DirectChannel fooChannel = new DirectChannel();
    fooChannel.setBeanName("fooChannel");
    DirectChannel barChannel = new DirectChannel();
    barChannel.setBeanName("barChannel");
    message = MessageHistory.write(message, fooChannel);
    message = MessageHistory.write(message, barChannel);
    messageStore.addMessage(message);
    message = messageStore.getMessage(message.getHeaders().getId());
    MessageHistory messageHistory = MessageHistory.read(message);
    assertNotNull(messageHistory);
    assertEquals(2, messageHistory.size());
    Properties fooChannelHistory = messageHistory.get(0);
    assertEquals("fooChannel", fooChannelHistory.get("name"));
    assertEquals("channel", fooChannelHistory.get("type"));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHistory(org.springframework.integration.history.MessageHistory) DirectChannel(org.springframework.integration.channel.DirectChannel) Properties(java.util.Properties) Test(org.junit.Test)

Example 72 with DirectChannel

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

the class WebSocketServerTests method testBrokerIsNotPresented.

@Test
public void testBrokerIsNotPresented() throws Exception {
    WebSocketInboundChannelAdapter webSocketInboundChannelAdapter = new WebSocketInboundChannelAdapter(Mockito.mock(ServerWebSocketContainer.class));
    webSocketInboundChannelAdapter.setOutputChannel(new DirectChannel());
    webSocketInboundChannelAdapter.setUseBroker(true);
    webSocketInboundChannelAdapter.setBeanFactory(Mockito.mock(BeanFactory.class));
    webSocketInboundChannelAdapter.setApplicationContext(Mockito.mock(ApplicationContext.class));
    try {
        webSocketInboundChannelAdapter.afterPropertiesSet();
        fail("IllegalStateException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("WebSocket Broker Relay isn't present in the application context;"));
    }
}
Also used : WebSocketInboundChannelAdapter(org.springframework.integration.websocket.inbound.WebSocketInboundChannelAdapter) ApplicationContext(org.springframework.context.ApplicationContext) ServerWebSocketContainer(org.springframework.integration.websocket.ServerWebSocketContainer) DirectChannel(org.springframework.integration.channel.DirectChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 73 with DirectChannel

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

the class PresenceListeningEndpointTests method testWithErrorChannel.

@Test
public void testWithErrorChannel() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    XMPPConnection connection = mock(XMPPConnection.class);
    bf.registerSingleton(XmppContextUtils.XMPP_CONNECTION_BEAN_NAME, connection);
    PresenceListeningEndpoint endpoint = new PresenceListeningEndpoint();
    DirectChannel outChannel = new DirectChannel();
    outChannel.subscribe(message -> {
        throw new RuntimeException("ooops");
    });
    PollableChannel errorChannel = new QueueChannel();
    endpoint.setBeanFactory(bf);
    endpoint.setOutputChannel(outChannel);
    endpoint.setErrorChannel(errorChannel);
    endpoint.afterPropertiesSet();
    RosterListener listener = (RosterListener) TestUtils.getPropertyValue(endpoint, "rosterListener");
    Presence presence = new Presence(Type.available);
    listener.presenceChanged(presence);
    ErrorMessage msg = (ErrorMessage) errorChannel.receive();
    assertSame(presence, ((MessagingException) msg.getPayload()).getFailedMessage().getPayload());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PollableChannel(org.springframework.messaging.PollableChannel) Presence(org.jivesoftware.smack.packet.Presence) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ErrorMessage(org.springframework.messaging.support.ErrorMessage) RosterListener(org.jivesoftware.smack.roster.RosterListener) Test(org.junit.Test)

Example 74 with DirectChannel

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

the class ApplicationTests method testWebSockets.

@Test
public void testWebSockets() throws InterruptedException {
    System.setProperty("local.server.port", this.port);
    ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("client-context.xml", org.springframework.integration.samples.websocket.standard.client.Application.class);
    DirectChannel webSocketInputChannel = ctx.getBean("webSocketInputChannel", DirectChannel.class);
    final CountDownLatch stopLatch = new CountDownLatch(2);
    webSocketInputChannel.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
            Object payload = message.getPayload();
            assertThat(payload, instanceOf(String.class));
            Date date = null;
            try {
                date = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.DEFAULT).parse((String) payload);
            } catch (ParseException e) {
                fail("fail to parse date");
            }
            assertThat(new Date().compareTo(date), greaterThanOrEqualTo(0));
            stopLatch.countDown();
        }
    });
    assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
    ctx.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) DirectChannel(org.springframework.integration.channel.DirectChannel) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ParseException(java.text.ParseException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 75 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-cloud-stream by spring-cloud.

the class AbstractBinderTests method testSendPojoReceivePojoWithStreamListener.

@SuppressWarnings("rawtypes")
@Test
public void testSendPojoReceivePojoWithStreamListener() throws Exception {
    StreamListenerMessageHandler handler = this.buildStreamListener(AbstractBinderTests.class, "echoStation", Station.class);
    Binder binder = getBinder();
    BindingProperties producerBindingProperties = createProducerBindingProperties(createProducerProperties());
    DirectChannel moduleOutputChannel = createBindableChannel("output", producerBindingProperties);
    BindingProperties consumerBindingProperties = createConsumerBindingProperties(createConsumerProperties());
    DirectChannel moduleInputChannel = createBindableChannel("input", consumerBindingProperties);
    Binding<MessageChannel> producerBinding = binder.bindProducer(String.format("bad%s0f", getDestinationNameDelimiter()), moduleOutputChannel, producerBindingProperties.getProducer());
    Binding<MessageChannel> consumerBinding = binder.bindConsumer(String.format("bad%s0f", getDestinationNameDelimiter()), "test-6", moduleInputChannel, consumerBindingProperties.getConsumer());
    Readings r1 = new Readings();
    r1.setCustomerid("123");
    r1.setStationid("XYZ");
    Readings r2 = new Readings();
    r2.setCustomerid("546");
    r2.setStationid("ABC");
    Station station = new Station();
    station.setReadings(Arrays.asList(r1, r2));
    Message<?> message = MessageBuilder.withPayload(station).setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON).build();
    moduleInputChannel.subscribe(handler);
    moduleOutputChannel.send(message);
    QueueChannel channel = (QueueChannel) handler.getOutputChannel();
    Message<Station> reply = (Message<Station>) channel.receive(5000);
    assertNotNull(reply);
    assertTrue(reply.getPayload() instanceof Station);
    producerBinding.unbind();
    consumerBinding.unbind();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) Readings(org.springframework.cloud.stream.binder.AbstractBinderTests.Station.Readings) StreamListenerMessageHandler(org.springframework.cloud.stream.binding.StreamListenerMessageHandler) Test(org.junit.Test)

Aggregations

DirectChannel (org.springframework.integration.channel.DirectChannel)215 Test (org.junit.Test)182 MessageChannel (org.springframework.messaging.MessageChannel)71 Message (org.springframework.messaging.Message)68 QueueChannel (org.springframework.integration.channel.QueueChannel)63 BeanFactory (org.springframework.beans.factory.BeanFactory)45 GenericMessage (org.springframework.messaging.support.GenericMessage)38 MessageHandler (org.springframework.messaging.MessageHandler)32 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)28 CountDownLatch (java.util.concurrent.CountDownLatch)27 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)26 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)25 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)23 HashMap (java.util.HashMap)22 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)22 MessagingException (org.springframework.messaging.MessagingException)18 Properties (java.util.Properties)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 SubscribableChannel (org.springframework.messaging.SubscribableChannel)15 Expression (org.springframework.expression.Expression)14