Example usage for org.springframework.integration.channel QueueChannel QueueChannel

List of usage examples for org.springframework.integration.channel QueueChannel QueueChannel

Introduction

In this page you can find the example usage for org.springframework.integration.channel QueueChannel QueueChannel.

Prototype

public QueueChannel(int capacity) 

Source Link

Document

Create a channel with the specified queue capacity.

Usage

From source file:biz.c24.io.spring.integration.config.FileSplitterTests.java

@Before
public void setUp() {
    feedChannel = new QueueChannel(100);
    transformer = new C24FileSplittingTransformer(feedChannel);
    resource = new ClassPathResource("datafixtures/10-lines.txt");
}

From source file:org.bpmscript.integration.spring.SpringIntegrationTest.java

public void testSpringIntegration() throws Exception {
    final CountDownLatch latch = new CountDownLatch(2);
    MessageBus messageBus = new MessageBus();
    QueueChannel oneChannel = new QueueChannel(10);
    QueueChannel twoChannel = new QueueChannel(10);
    messageBus.registerChannel("onechannel", oneChannel);
    messageBus.registerChannel("twochannel", twoChannel);
    messageBus.registerHandler("onehandler", new MessageHandler() {

        public Message<?> handle(Message<?> message) {
            log.info(message.getPayload());
            latch.countDown();//www .  ja v  a  2s  .  c om
            return new StringMessage("Hi...");
        }

    }, new Subscription(oneChannel));
    messageBus.registerHandler("twohandler", new MessageHandler() {

        public Message<?> handle(Message<?> message) {
            log.info(message.getPayload());
            latch.countDown();
            return null;
        }

    }, new Subscription(twoChannel));
    messageBus.start();
    StringMessage stringMessage = new StringMessage("Hello World!");
    stringMessage.getHeader().setReturnAddress("twochannel");
    oneChannel.send(stringMessage);
    assertTrue(latch.await(2, TimeUnit.SECONDS));
    messageBus.stop();
}

From source file:eric.bottard.tis100.Runner.java

public Runner(int rows, int columns, String specificationFile, File solution) throws IOException {

    Specification specification = new LuaSpecification(specificationFile, rows, columns);

    List<String> nodeSources = loadSolution(solution, specification.getLayout());

    this.rows = rows;
    this.columns = columns;

    verticalChannels = new MessageChannel[columns * (rows + 1) * 2];
    horizontalChannels = new MessageChannel[rows * (columns + 1) * 2];

    // channels[2*x] = ltr / down-to-up
    // channels[2*x + 1] = rtl / up-to-down
    for (int row = 0; row <= rows; row++) {
        for (int column = 0; column < columns; column++) {
            verticalChannels[(row * columns + column) * 2] = (row == 0 || row == rows) ? new NullChannel()
                    : new RendezvousChannel();
            if (row == 0) {
                verticalChannels[(row * columns + column) * 2 + 1] = new QueueChannel(40);
            } else if (row == rows) {
                verticalChannels[(row * columns + column) * 2 + 1] = new PublishSubscribeChannel();
            } else {
                verticalChannels[(row * columns + column) * 2 + 1] = new RendezvousChannel();
            }// w w  w  . j  av  a  2 s.co  m
        }
    }

    for (int row = 0; row < rows; row++) {
        for (int column = 0; column <= columns; column++) {
            horizontalChannels[(column * rows + row) * 2] = (column == 0 || column == columns)
                    ? new NullChannel()
                    : new RendezvousChannel();
            horizontalChannels[(column * rows + row) * 2 + 1] = (column == 0 || column == columns)
                    ? new NullChannel()
                    : new RendezvousChannel();
        }
    }

    Thread[] threads = new Thread[rows * columns];
    Object mutex = new Object();

    for (int row = 0; row < rows; row++) {
        for (int column = 0; column < columns; column++) {
            final SpringNode node = NodeFactory.buildNode(nodeSources.get(row * columns + column));
            final NodePrettyPrinter printer = new NodePrettyPrinter(4 + row * 19,
                    SpecificationPrettyPrinter.WIDTH + 10 + column * 37, node);
            Ports ports = new PortsMapping(row, column);
            node.setPorts(ports);
            nodes.add(node);
            threads[row * columns + column] = new Thread() {
                @Override
                public void run() {

                    boolean more;
                    do {
                        synchronized (mutex) {
                            printer.draw(System.out);
                        }
                        try {
                            Thread.sleep(150);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        more = node.tick();
                    } while (more);
                }
            };
        }
    }

    List<Integer>[] actuals = new List[columns];
    SpecificationPrettyPrinter specificationPrettyPrinter = new SpecificationPrettyPrinter(specification, 2, 2,
            actuals);

    for (int i = 0; i < columns; i++) {
        Specification.Stream stream = specification.getInputStreams()[i];
        if (stream != null) {
            for (Integer in : stream.getData()) {
                verticalChannels[1 + 2 * i].send(new GenericMessage<>(in));
            }
        }
        stream = specification.getOutputStreams()[i];
        final List<Integer> actual = actuals[i] = new ArrayList<>();
        if (stream != null) {
            ((SubscribableChannel) verticalChannels[1 + 2 * (i + rows * columns)])
                    .subscribe(new MessageHandler() {
                        @Override
                        public void handleMessage(Message<?> message) throws MessagingException {
                            actual.add((Integer) message.getPayload());
                            synchronized (mutex) {
                                specificationPrettyPrinter.draw(System.out);
                            }
                        }
                    });
        }
    }
    synchronized (mutex) {
        specificationPrettyPrinter.draw(System.out);
    }

    for (int i = 0; i < rows * columns; i++) {
        threads[i].start();
    }

}

From source file:hello.CrawlerApp.java

@Bean
public MessageChannel channel1() {
    return new QueueChannel(10);
}

From source file:hello.CrawlerApp.java

@Bean
public MessageChannel channel4() {
    return new QueueChannel(10);
}

From source file:org.springframework.integration.channel.QueueChannelTests.java

@Test
public void testImmediateSend() {
    QueueChannel channel = new QueueChannel(3);
    boolean result1 = channel.send(new GenericMessage<String>("test-1"));
    assertTrue(result1);//w  ww. j a v a  2  s. co m
    boolean result2 = channel.send(new GenericMessage<String>("test-2"), 100);
    assertTrue(result2);
    boolean result3 = channel.send(new GenericMessage<String>("test-3"), 0);
    assertTrue(result3);
    boolean result4 = channel.send(new GenericMessage<String>("test-4"), 0);
    assertFalse(result4);
}

From source file:org.springframework.integration.channel.QueueChannelTests.java

@Test
public void testBlockingSendWithNoTimeout() throws Exception {
    final QueueChannel channel = new QueueChannel(1);
    boolean result1 = channel.send(new GenericMessage<String>("test-1"));
    assertTrue(result1);/*from  w  w w . j  av  a 2 s.  com*/
    final AtomicBoolean sendInterrupted = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            channel.send(new GenericMessage<String>("test-2"));
            sendInterrupted.set(true);
            latch.countDown();
        }
    });
    t.start();
    assertFalse(sendInterrupted.get());
    t.interrupt();
    latch.await();
    assertTrue(sendInterrupted.get());
}

From source file:org.springframework.integration.channel.QueueChannelTests.java

@Test
public void testBlockingSendWithTimeout() throws Exception {
    final QueueChannel channel = new QueueChannel(1);
    boolean result1 = channel.send(new GenericMessage<String>("test-1"));
    assertTrue(result1);//from  ww w .ja v  a2 s  .  co  m
    final AtomicBoolean sendInterrupted = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            channel.send(new GenericMessage<String>("test-2"), 10000);
            sendInterrupted.set(true);
            latch.countDown();
        }
    });
    t.start();
    assertFalse(sendInterrupted.get());
    t.interrupt();
    latch.await();
    assertTrue(sendInterrupted.get());
}

From source file:org.springframework.integration.channel.QueueChannelTests.java

@Test
public void testClear() {
    QueueChannel channel = new QueueChannel(2);
    GenericMessage<String> message1 = new GenericMessage<String>("test1");
    GenericMessage<String> message2 = new GenericMessage<String>("test2");
    GenericMessage<String> message3 = new GenericMessage<String>("test3");
    assertTrue(channel.send(message1));// ww w .  j ava  2 s  .c o  m
    assertTrue(channel.send(message2));
    assertFalse(channel.send(message3, 0));
    List<Message<?>> clearedMessages = channel.clear();
    assertNotNull(clearedMessages);
    assertEquals(2, clearedMessages.size());
    assertTrue(channel.send(message3));
}

From source file:org.springframework.integration.channel.QueueChannelTests.java

@Test
public void testPurge() {
    QueueChannel channel = new QueueChannel(2);
    long minute = 60 * 1000;
    long time = System.currentTimeMillis();
    long past = time - minute;
    long future = time + minute;
    Message<String> expiredMessage = MessageBuilder.withPayload("test1").setExpirationDate(past).build();
    Message<String> unexpiredMessage = MessageBuilder.withPayload("test2").setExpirationDate(future).build();
    assertTrue(channel.send(expiredMessage, 0));
    assertTrue(channel.send(unexpiredMessage, 0));
    assertFalse(channel.send(new GenericMessage<String>("atCapacity"), 0));
    List<Message<?>> purgedMessages = channel.purge(new UnexpiredMessageSelector());
    assertNotNull(purgedMessages);//from  www .  j ava2  s.co  m
    assertEquals(1, purgedMessages.size());
    assertTrue(channel.send(new GenericMessage<String>("roomAvailable"), 0));
}