uk.co.jemos.experiments.integration.HelloWorldApp.java Source code

Java tutorial

Introduction

Here is the source code for uk.co.jemos.experiments.integration.HelloWorldApp.java

Source

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.jemos.experiments.integration;

import org.apache.log4j.Logger;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;

/**
 * Demonstrates a basic Message Endpoint that simply prepends a greeting
 * ("Hello ") to an inbound String payload from a Message. This is a very
 * low-level example, using Message Channels directly for both input and
 * output. Notice that the output channel has a queue sub-element. It is
 * therefore a PollableChannel and its consumers must invoke receive() as
 * demonstrated below.
 * <p>
 * View the configuration of the channels and the endpoint (a &lt;service-activator/&gt;
 * element) in 'helloWorldDemo.xml' within this same package.
 *
 * @author Mark Fisher
 * @author Oleg Zhurakousky
 */
public class HelloWorldApp {

    private static Logger logger = Logger.getLogger(HelloWorldApp.class);

    public static void main(String[] args) {

        AbstractApplicationContext context = new ClassPathXmlApplicationContext(
                "/META-INF/spring/integration/helloWorldDemo.xml", HelloWorldApp.class);
        MessageChannel inputChannel = context.getBean("podamInputChannel", MessageChannel.class);

        Message<Object> intMessage = MessageBuilder.withPayload(new Object())
                .setHeader("type", int.class.toString()).build();
        Message<Object> boolMessage = MessageBuilder.withPayload(new Object())
                .setHeader("type", boolean.class.toString()).build();
        Message<Object> stringMessage = MessageBuilder.withPayload(new Object())
                .setHeader("type", String.class.getName()).build();

        MessagingTemplate template = new MessagingTemplate();
        Message reply = template.sendAndReceive(inputChannel, intMessage);
        logger.info(reply.getPayload());
        reply = template.sendAndReceive(inputChannel, boolMessage);
        logger.info(reply.getPayload());
        reply = template.sendAndReceive(inputChannel, stringMessage);
        logger.info(reply.getPayload());

        context.close();

    }

}