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

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

Introduction

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

Prototype

public PublishSubscribeChannel() 

Source Link

Document

Create a PublishSubscribeChannel that will invoke the handlers in the message sender's thread.

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   ww  w  .j a v a2 s  .c  om
        }
    }

    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.nayidisha.slowglow.config.SpringMessagingConfig.java

/**
 * SubscribableChannel for Axon CQRS to use
 * // w  ww  . j  av a2 s  .  c  o m
 * @return
 */
@Bean(name = "webSocketInputChannel")
public PublishSubscribeChannel webSocketInputChannel() {
    PublishSubscribeChannel channel = new PublishSubscribeChannel();
    List<ChannelInterceptor> list = new ArrayList<>(1);
    list.add(messageSelectingInterceptor());
    channel.setInterceptors(list);

    //        channel.setDatatypes(Object.class); // we've defined it using the PayloadTypeSelector instead and injected it as an interceptor above
    return channel;
}

From source file:ru.jts_dev.authserver.config.AuthIntegrationConfig.java

/**
 * Channel for outgoing packets
 *
 * @return - channel
 */
@Bean
public MessageChannel tcpOutChannel() {
    return new PublishSubscribeChannel();
}

From source file:com.nayidisha.slowglow.config.SpringMessagingConfig.java

/**
 * This channel can be used to monitor messages from our main channel
 * without interrupting it.//from   ww  w. j a  v  a  2 s .c  o m
 * 
 * @return
 */
@Bean(name = "monitoringChannel")
public PublishSubscribeChannel monitoringChannel() {
    PublishSubscribeChannel channel = new PublishSubscribeChannel();
    List<ChannelInterceptor> list = new ArrayList<>(1);
    list.add(wireTap());
    channel.setInterceptors(list);

    return channel;
}

From source file:org.springframework.cloud.stream.binder.AbstractMessageChannelBinder.java

/**
 * Register an error channel for the destination when an async send error is received.
 * Bridge the channel to the global error channel (if present).
 * @param destination the destination./* w w w . j  av  a  2 s . c o  m*/
 * @return the channel.
 */
private SubscribableChannel registerErrorInfrastructure(ProducerDestination destination) {
    ConfigurableListableBeanFactory beanFactory = getApplicationContext().getBeanFactory();
    String errorChannelName = errorsBaseName(destination);
    SubscribableChannel errorChannel = null;
    if (getApplicationContext().containsBean(errorChannelName)) {
        Object errorChannelObject = getApplicationContext().getBean(errorChannelName);
        if (!(errorChannelObject instanceof SubscribableChannel)) {
            throw new IllegalStateException(
                    "Error channel '" + errorChannelName + "' must be a SubscribableChannel");
        }
        errorChannel = (SubscribableChannel) errorChannelObject;
    } else {
        errorChannel = new PublishSubscribeChannel();
        beanFactory.registerSingleton(errorChannelName, errorChannel);
        errorChannel = (PublishSubscribeChannel) beanFactory.initializeBean(errorChannel, errorChannelName);
    }
    MessageChannel defaultErrorChannel = null;
    if (getApplicationContext().containsBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)) {
        defaultErrorChannel = getApplicationContext().getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME,
                MessageChannel.class);
    }
    if (defaultErrorChannel != null) {
        BridgeHandler errorBridge = new BridgeHandler();
        errorBridge.setOutputChannel(defaultErrorChannel);
        errorChannel.subscribe(errorBridge);
        String errorBridgeHandlerName = getErrorBridgeName(destination);
        beanFactory.registerSingleton(errorBridgeHandlerName, errorBridge);
        beanFactory.initializeBean(errorBridge, errorBridgeHandlerName);
    }
    return errorChannel;
}

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

@Test
public void testPubSubChannelLoggingWithMoreThenOneSubscriber() {
    final PublishSubscribeChannel channel = new PublishSubscribeChannel();
    channel.setBeanName("pubSubChannel");

    final Log logger = mock(Log.class);
    when(logger.isInfoEnabled()).thenReturn(true);
    ReflectionUtils.doWithFields(AbstractMessageChannel.class, new FieldCallback() {

        public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
            if ("logger".equals(field.getName())) {
                field.setAccessible(true);
                field.set(channel, logger);
            }/*from  ww w  .  ja v a 2  s.co m*/
        }
    });
    channel.subscribe(mock(MessageHandler.class));
    channel.subscribe(mock(MessageHandler.class));
    verify(logger, times(2)).info(Mockito.anyString());
}