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

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

Introduction

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

Prototype

public RendezvousChannel() 

Source Link

Usage

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();
            }//from  w  w  w  .  ja  v  a  2s.  c o 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:com.seajas.search.profiler.service.testing.TestingService.java

/**
 * Dispatch a type-specified modifier test request and wait for the result.
 * //w  w  w.  ja v a  2  s  .  co  m
 * @param testFeed
 * @param type
 * @return Map<String, Boolean>
 * @throws TestingException
 */
@SuppressWarnings("unchecked")
private TestResult testModifier(final TestElement testFeed, final TestType type) throws TestingException {
    final UUID sessionUUID = UUID.randomUUID();

    final RendezvousChannel temporaryChannel = new RendezvousChannel();

    final MessageHandler messageHandler = new MessageHandler() {
        @Override
        public void handleMessage(final Message<?> message) {
            String messageUUID = message.getHeaders().get(MessageConstants.HEADER_RENDEZVOUS_UUID,
                    String.class);

            if (sessionUUID.toString().equals(messageUUID))
                temporaryChannel.send(message);
        }
    };

    testingChannel.subscribe(messageHandler);

    try {
        injectionService.inject(testFeed, GroupIdDecorator.decorate(testFeed.getTestingUri().getHost()),
                type.name(), sessionUUID.toString());

        // Now wait for read out the rendezvous-channel until we receive the response

        Message<TestResult> message = (Message<TestResult>) temporaryChannel.receive(receiveTimeout);

        if (message != null)
            return message.getPayload();

        throw new TestingException("Receiving the testing response failed or timed out");
    } finally {
        testingChannel.unsubscribe(messageHandler);
    }
}

From source file:com.seajas.search.profiler.service.testing.TestingService.java

/**
 * Dispatch a feed-testing request, where the result is evaluated on the contender-side to be of type RSS - directly or indirectly.
 * //  w  w  w . j  a  v a 2s . co  m
 * <ul>
 * <li>When a TestingException is thrown, something went wrong or no URLs could be found</li>
 * <li>When one entry with a boolean value of 'true' is returned, the URL concerns a direct RSS feed</li>
 * <li>When one or more entry with a boolean value of 'false' is returned, the URL concerns an indirect RSS feed with one or more links</li>
 * </ul>
 * 
 * @param uri
 * @return TestResult
 * @throws TestingException
 */
@SuppressWarnings("unchecked")
public Map<String, Boolean> exploreUri(final URI uri) throws TestingException {
    final UUID sessionUUID = UUID.randomUUID();

    final RendezvousChannel temporaryChannel = new RendezvousChannel();

    final MessageHandler messageHandler = new MessageHandler() {
        @Override
        public void handleMessage(final Message<?> message) {
            String messageUUID = message.getHeaders().get(MessageConstants.HEADER_RENDEZVOUS_UUID,
                    String.class);

            if (sessionUUID.toString().equals(messageUUID))
                temporaryChannel.send(message);
        }
    };

    testingChannel.subscribe(messageHandler);

    try {
        TestElement testElement = new TestElementImpl(uri, null, null, null, null);

        if (logger.isInfoEnabled())
            logger.info("Injecting element with exploration URI '" + uri + "'");

        injectionService.inject(testElement, GroupIdDecorator.decorate(uri.getHost()), TestType.EXPLORE.name(),
                sessionUUID.toString());

        // Now wait for read out the rendezvous-channel until we receive the response

        Message<TestResult> message = (Message<TestResult>) temporaryChannel.receive(receiveTimeout);

        if (message != null) {
            TestResult result = message.getPayload();

            if (!result.isSuccess())
                throw new TestingException(
                        "No feeds - direct or indirect - were retrieved: " + result.getStatusMessage());
            else
                return result.getResult();
        }

        throw new TestingException("Test response aborted by timeout or interrupt.");
    } finally {
        testingChannel.unsubscribe(messageHandler);
    }
}