Example usage for org.springframework.messaging MessageChannel send

List of usage examples for org.springframework.messaging MessageChannel send

Introduction

In this page you can find the example usage for org.springframework.messaging MessageChannel send.

Prototype

default boolean send(Message<?> message) 

Source Link

Document

Send a Message to this channel.

Usage

From source file:demo.HttpIntegrationApplication.java

public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext ctx = SpringApplication.run(HttpIntegrationApplication.class);
    Thread.sleep(2000);//  ww w.j  a v  a 2 s.  c  om
    MessageChannel channel = ctx.getBean("input", MessageChannel.class);
    channel.send(MessageBuilder
            .withPayload("{\"userId\": \"invalid@mail.com\",\"password\": \"wrongpassword\"}").build());
}

From source file:com.mycompany.integration.NewMain.java

/**
 * @param args the command line arguments
 *//*ww  w . j  a v a 2  s.  c  o  m*/
public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-integration.xml");

    MessageChannel channel = context.getBean("toRabbit", MessageChannel.class);

    for (int i = 0; i < 10; i++) {
        channel.send(build(UUID.randomUUID().toString()));
    }
}

From source file:com.tvd.common.channel.HelloWorldExample.java

public static void main(String args[]) {
    String cfg = "channel/context.xml";
    @SuppressWarnings("resource")
    ApplicationContext context = new ClassPathXmlApplicationContext(cfg);
    MessageChannel channel = context.getBean("names", MessageChannel.class);
    Message<String> message = MessageBuilder.withPayload("World").build();
    channel.send(message);
}

From source file:helloworld.HelloWorldApp.java

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-integration-helloword-context.xml");
    MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
    PollableChannel outputChannel = context.getBean("outputChannel", PollableChannel.class);
    inputChannel.send(new GenericMessage<String>("World"));
    System.out.println("==> HelloWorldDemo: " + outputChannel.receive(0).getPayload());
}

From source file:org.lottery.common.message.ProducerTest.java

@Test
public void testSend() {
    final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CONFIG);
    ctx.start();//w  w w  .j a v  a  2s . c om

    final MessageChannel channel = ctx.getBean("common-message.producer", MessageChannel.class);

    channel.send(MessageBuilder.withPayload("from messageChannel" + System.currentTimeMillis())
            .setHeader("messageKey", "key").setHeader("topic", "test").build());

    MessageProducer messageProducer = ctx.getBean(MessageProducer.class);
    messageProducer.send("test", "from messageProducer" + System.currentTimeMillis());
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    ctx.close();
}

From source file:com.aeg.ims.ftp.SftpOutboundTransferSample.java

@Test
public void testOutbound() throws Exception {

    final String sourceFileName = "README.md";
    final String destinationFileName = sourceFileName + "_foo";

    final ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(
            "/META-INF/spring/integration/SftpOutboundTransferSample-context.xml",
            SftpOutboundTransferSample.class);
    @SuppressWarnings("unchecked")
    SessionFactory<LsEntry> sessionFactory = ac.getBean(CachingSessionFactory.class);
    RemoteFileTemplate<LsEntry> template = new RemoteFileTemplate<LsEntry>(sessionFactory);
    //SftpTestUtils.createTestFiles(template); // Just the directory

    try {/*from   w  ww  .  ja  va  2s  . c  o  m*/
        final File file = new File(sourceFileName);

        Assert.isTrue(file.exists(), String.format("File '%s' does not exist.", sourceFileName));

        final Message<File> message = MessageBuilder.withPayload(file).build();
        final MessageChannel inputChannel = ac.getBean("inputChannel", MessageChannel.class);

        inputChannel.send(message);
        Thread.sleep(2000);

        Assert.isTrue(SftpTestUtils.fileExists(template, destinationFileName));

        System.out.println(String.format(
                "Successfully transferred '%s' file to a " + "remote location under the name '%s'",
                sourceFileName, destinationFileName));
    } finally {
        //SftpTestUtils.cleanUp(template, destinationFileName);
        ac.close();
    }
}

From source file:com.acme.ModuleConfigurationTest.java

@Test
public void test() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    Properties properties = new Properties();
    properties.put("prefix", "foo");
    properties.put("suffix", "bar");
    context.getEnvironment().getPropertySources().addLast(new PropertiesPropertySource("options", properties));
    context.register(TestConfiguration.class);
    context.refresh();/*from w w w  .  j a v  a2 s. c  om*/

    MessageChannel input = context.getBean("input", MessageChannel.class);
    SubscribableChannel output = context.getBean("output", SubscribableChannel.class);

    final AtomicBoolean handled = new AtomicBoolean();
    output.subscribe(new MessageHandler() {
        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            handled.set(true);
            assertEquals("foohellobar", message.getPayload());
        }
    });
    input.send(new GenericMessage<String>("hello"));
    assertTrue(handled.get());
}

From source file:ch.rasc.wampspring.config.WampSubProtocolHandler.java

@Override
public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus, MessageChannel outputChannel) {
    /*// www  . j  a va2s  .  co m
     * To cleanup we send an internal messages to the handlers. It might be possible
     * that this is an unexpected session end and the client did not unsubscribe his
     * subscriptions.
     */
    WampMessage message = UnsubscribeMessage.createCleanupMessage(session);

    try {
        WampSessionContextHolder.setAttributesFromMessage(message);
        outputChannel.send(message);
    } finally {
        WampSessionContextHolder.resetAttributes();
        message.getWampSession().sessionCompleted();
    }
}

From source file:io.pivotal.poc.dispatcher.MessageDispatcher.java

private String sendMessage(String topic, Object body, HttpHeaders requestHeaders) {
    MessageChannel channel = resolver.resolveDestination(topic + ".input");
    MessageBuilder<?> builder = MessageBuilder.withPayload(body);
    builder.setHeader(MessageHeaders.CONTENT_TYPE, requestHeaders.getContentType());
    for (Map.Entry<String, List<String>> entry : requestHeaders.entrySet()) {
        String headerName = entry.getKey();
        if (requestHeadersToMap.contains(headerName)) {
            builder.setHeaderIfAbsent(headerName,
                    StringUtils.collectionToCommaDelimitedString(entry.getValue()));
        }//from w  w  w.  j a va  2s  .c o  m
    }
    Message<?> message = builder.build();
    channel.send(message);
    return message.getHeaders().getId().toString();
}

From source file:ch.rasc.wampspring.config.WampSubProtocolHandler.java

/**
 * Handle incoming WebSocket messages from clients.
 *///from ww w. j a v  a 2s.c o  m
@Override
public void handleMessageFromClient(WebSocketSession session, WebSocketMessage<?> webSocketMessage,
        MessageChannel outputChannel) {

    Assert.isInstanceOf(TextMessage.class, webSocketMessage);
    WampMessage wampMessage = null;
    try {
        wampMessage = WampMessage.fromJson(session, this.jsonFactory,
                ((TextMessage) webSocketMessage).getPayload());
    } catch (Throwable ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Failed to parse " + webSocketMessage + " in session " + session.getId() + ".", ex);
        }
        return;
    }

    try {
        WampSessionContextHolder.setAttributesFromMessage(wampMessage);
        outputChannel.send(wampMessage);
    } catch (Throwable ex) {
        logger.error("Failed to send client message to application via MessageChannel" + " in session "
                + session.getId() + ".", ex);

        if (wampMessage != null && wampMessage instanceof CallMessage) {

            CallErrorMessage callErrorMessage = new CallErrorMessage((CallMessage) wampMessage, "",
                    ex.toString());

            try {
                String json = callErrorMessage.toJson(this.jsonFactory);
                session.sendMessage(new TextMessage(json));
            } catch (Throwable t) {
                // Could be part of normal workflow (e.g. browser tab closed)
                logger.debug("Failed to send error to client.", t);
            }

        }
    } finally {
        WampSessionContextHolder.resetAttributes();
    }
}