Package org.springframework.integration.channel

Examples of org.springframework.integration.channel.DirectChannel


  }

  @Test
  public void resolveQueueChannel() {
    MessageChannel registered = resolver.resolveDestination("queue:foo");
    DirectChannel testChannel = new DirectChannel();
    final CountDownLatch latch = new CountDownLatch(1);
    final List<Message<?>> received = new ArrayList<Message<?>>();
    testChannel.subscribe(new MessageHandler() {

      @Override
      public void handleMessage(Message<?> message) throws MessagingException {
        received.add(message);
        latch.countDown();
View Full Code Here


  @Test
  public void propertyPassthrough() {
    Properties properties = new Properties();
    MessageBus bus = mock(MessageBus.class);
    doReturn(new DirectChannel()).when(bus).bindDynamicProducer("queue:foo", properties);
    doReturn(new DirectChannel()).when(bus).bindDynamicPubSubProducer("topic:bar", properties);
    MessageBusAwareChannelResolver resolver = new MessageBusAwareChannelResolver(bus, properties);
    BeanFactory beanFactory = new DefaultListableBeanFactory();
    resolver.setBeanFactory(beanFactory);
    resolver.resolveDestination("queue:foo");
    resolver.resolveDestination("topic:bar");
View Full Code Here

  protected AbstractTestMessageBus<?> testMessageBus;

  @Test
  public void testClean() throws Exception {
    MessageBus messageBus = getMessageBus();
    messageBus.bindProducer("foo.0", new DirectChannel(), null);
    messageBus.bindConsumer("foo.0", new DirectChannel(), null);
    messageBus.bindProducer("foo.1", new DirectChannel(), null);
    messageBus.bindConsumer("foo.1", new DirectChannel(), null);
    messageBus.bindProducer("foo.2", new DirectChannel(), null);
    Collection<?> bindings = getBindings(messageBus);
    assertEquals(5, bindings.size());
    messageBus.unbindProducers("foo.0");
    assertEquals(4, bindings.size());
    messageBus.unbindConsumers("foo.0");
View Full Code Here

  }

  @Test
  public void testSendAndReceive() throws Exception {
    MessageBus messageBus = getMessageBus();
    DirectChannel moduleOutputChannel = new DirectChannel();
    QueueChannel moduleInputChannel = new QueueChannel();
    messageBus.bindProducer("foo.0", moduleOutputChannel, null);
    messageBus.bindConsumer("foo.0", moduleInputChannel, null);
    Message<?> message = MessageBuilder.withPayload("foo").setHeader(MessageHeaders.CONTENT_TYPE, "foo/bar").build();
    // Let the consumer actually bind to the producer before sending a msg
    busBindUnbindLatency();
    moduleOutputChannel.send(message);
    Message<?> inbound = moduleInputChannel.receive(5000);
    assertNotNull(inbound);
    assertEquals("foo", inbound.getPayload());
    assertNull(inbound.getHeaders().get(MessageBusSupport.ORIGINAL_CONTENT_TYPE_HEADER));
    assertEquals("foo/bar", inbound.getHeaders().get(MessageHeaders.CONTENT_TYPE));
View Full Code Here

  }

  @Test
  public void testSendAndReceiveNoOriginalContentType() throws Exception {
    MessageBus messageBus = getMessageBus();
    DirectChannel moduleOutputChannel = new DirectChannel();
    QueueChannel moduleInputChannel = new QueueChannel();
    messageBus.bindProducer("bar.0", moduleOutputChannel, null);
    messageBus.bindConsumer("bar.0", moduleInputChannel, null);
    busBindUnbindLatency();

    Message<?> message = MessageBuilder.withPayload("foo").build();
    moduleOutputChannel.send(message);
    Message<?> inbound = moduleInputChannel.receive(5000);
    assertNotNull(inbound);
    assertEquals("foo", inbound.getPayload());
    assertNull(inbound.getHeaders().get(MessageBusSupport.ORIGINAL_CONTENT_TYPE_HEADER));
    assertNull(inbound.getHeaders().get(MessageHeaders.CONTENT_TYPE));
View Full Code Here

  }

  @Test
  public void testSendAndReceivePubSub() throws Exception {
    MessageBus messageBus = getMessageBus();
    DirectChannel moduleOutputChannel = new DirectChannel();
    // Test pub/sub by emulating how StreamPlugin handles taps
    DirectChannel tapChannel = new DirectChannel();
    QueueChannel moduleInputChannel = new QueueChannel();
    QueueChannel module2InputChannel = new QueueChannel();
    QueueChannel module3InputChannel = new QueueChannel();
    messageBus.bindProducer("baz.0", moduleOutputChannel, null);
    messageBus.bindConsumer("baz.0", moduleInputChannel, null);
View Full Code Here

  }

  @Test
  public void createInboundPubSubBeforeOutboundPubSub() throws Exception {
    MessageBus messageBus = getMessageBus();
    DirectChannel moduleOutputChannel = new DirectChannel();
    // Test pub/sub by emulating how StreamPlugin handles taps
    DirectChannel tapChannel = new DirectChannel();
    QueueChannel moduleInputChannel = new QueueChannel();
    QueueChannel module2InputChannel = new QueueChannel();
    QueueChannel module3InputChannel = new QueueChannel();
    // Create the tap first
    messageBus.bindPubSubConsumer("tap:baz.http", module2InputChannel, null);
View Full Code Here

    doRegisterConsumer(name, name, moduleInputChannel, adapter, new RedisPropertiesAccessor(properties));
  }

  private void doRegisterConsumer(String bindingName, String channelName, MessageChannel moduleInputChannel,
      MessageProducerSupport adapter, RedisPropertiesAccessor properties) {
    DirectChannel bridgeToModuleChannel = new DirectChannel();
    bridgeToModuleChannel.setBeanFactory(this.getBeanFactory());
    bridgeToModuleChannel.setBeanName(channelName + ".bridge");
    MessageChannel bridgeInputChannel = addRetryIfNeeded(channelName, bridgeToModuleChannel, properties);
    adapter.setOutputChannel(bridgeInputChannel);
    adapter.setBeanName("inbound." + bindingName);
    adapter.afterPropertiesSet();
    Binding consumerBinding = Binding.forConsumer(bindingName, adapter, moduleInputChannel, properties);
    addBinding(consumerBinding);
    ReceivingHandler convertingBridge = new ReceivingHandler();
    convertingBridge.setOutputChannel(moduleInputChannel);
    convertingBridge.setBeanName(channelName + ".bridge.handler");
    convertingBridge.afterPropertiesSet();
    bridgeToModuleChannel.subscribe(convertingBridge);
    consumerBinding.start();
  }
View Full Code Here

    final RetryTemplate retryTemplate = buildRetryTemplateIfRetryEnabled(properties);
    if (retryTemplate == null) {
      return bridgeToModuleChannel;
    }
    else {
      DirectChannel channel = new DirectChannel() {

        @Override
        protected boolean doSend(final Message<?> message, final long timeout) {
          try {
            return retryTemplate.execute(new RetryCallback<Boolean, Exception>() {

              @Override
              public Boolean doWithRetry(RetryContext context) throws Exception {
                return bridgeToModuleChannel.send(message, timeout);
              }

            }, new RecoveryCallback<Boolean>() {

              /**
               * Send the failed message to 'ERRORS:[name]'.
               */
              @Override
              public Boolean recover(RetryContext context) throws Exception {
                logger.error(
                    "Failed to deliver message; retries exhausted; message sent to queue 'ERRORS:name'",
                    context.getLastThrowable());
                errorAdapter.handleMessage(getMessageBuilderFactory().fromMessage(message)
                    .setHeader(ERROR_HEADER, "ERRORS:" + name)
                    .build());
                return true;
              }

            });
          }
          catch (Exception e) {
            logger.error("Failed to deliver message", e);
            return false;
          }
        }

      };
      channel.setBeanName(name + ".bridge");
      return channel;
    }
  }
View Full Code Here

  public void testDirectBinding() throws Exception {
    MessageBus bus = getMessageBus();
    Properties properties = new Properties();
    properties.setProperty(BusProperties.DIRECT_BINDING_ALLOWED, "true");

    DirectChannel moduleInputChannel = new DirectChannel();
    moduleInputChannel.setBeanName("direct.input");
    DirectChannel moduleOutputChannel = new DirectChannel();
    moduleOutputChannel.setBeanName("direct.output");
    bus.bindConsumer("direct.0", moduleInputChannel, null);
    bus.bindProducer("direct.0", moduleOutputChannel, properties);

    final AtomicReference<Thread> caller = new AtomicReference<Thread>();
    final AtomicInteger count = new AtomicInteger();
    moduleInputChannel.subscribe(new MessageHandler() {

      @Override
      public void handleMessage(Message<?> message) throws MessagingException {
        caller.set(Thread.currentThread());
        count.incrementAndGet();
      }
    });

    moduleOutputChannel.send(new GenericMessage<String>("foo"));
    moduleOutputChannel.send(new GenericMessage<String>("foo"));

    assertNotNull(caller.get());
    assertSame(Thread.currentThread(), caller.get());
    assertEquals(2, count.get());
    assertNull(spyOn("direct.0").receive(true));

    // Remove direct binding and bind producer to the bus
    bus.unbindConsumers("direct.0");
    busBindUnbindLatency();

    Spy spy = spyOn("direct.0");
    count.set(0);
    moduleOutputChannel.send(new GenericMessage<String>("bar"));
    moduleOutputChannel.send(new GenericMessage<String>("baz"));
    Object bar = spy.receive(false);
    assertEquals("bar", bar);
    Object baz = spy.receive(false);
    assertEquals("baz", baz);
    assertEquals(0, count.get());

    // Unbind producer from bus and bind directly again
    caller.set(null);
    bus.bindConsumer("direct.0", moduleInputChannel, null);
    moduleOutputChannel.send(new GenericMessage<String>("foo"));
    moduleOutputChannel.send(new GenericMessage<String>("foo"));
    assertNotNull(caller.get());
    assertSame(Thread.currentThread(), caller.get());
    assertEquals(2, count.get());
    assertNull(spy.receive(true));
View Full Code Here

TOP

Related Classes of org.springframework.integration.channel.DirectChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.