Example of manually publishing a message to an outbound channel


In this article, we will learn how to manually create a message and publish to an outbound channel.
We will create an Employee object and publish it to a channel for further processing. Next, a Service Activator will pickup the message from the channel to process it.

Manually create and publish message to channel

In our example, we only need one channel and an endpoint. In applicationContext.xml, we will configure our channel and endpoint.
First declare the channel processEmpChannel to which the message will be published.
Next, will configure the bean that will build and publish our message. SpringIntegrationChannelExample is the class and postEmployeeMsg is the method that will create and publish the message. Since it needs to know the channel to publish the message, we will have to inject the channel bean to SpringIntegrationChannelExample.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd">

	<int:channel id="processEmpChannel">
		<int:queue />

	<bean id="springIntChannelExample"
		<property name="out" ref="processEmpChannel" />
	<bean id="empProcessor"

	<int:service-activator input-channel="processEmpChannel" ref="empProcessor">
		<int:poller fixed-rate="500"/>

The actual payload to be sent needs to be encapsulated in a Message and published to the channel processEmpChannel. The service-activator endpoint will then pick up the message as soon as it arrives on the processEmpChannel and calls the postEmployeeMsg method on class SpringIntegrationChannelExample. Since there is only one method in the bean, we don’t have to mention the method name else we must mention the method name to be invoked in method attribute.

We will use the utility class MessageBuilder and call its static method withPayload to build the message.

MessageChannel interface has send method to send data using a Message object


package com.javarticles.spring.integration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;

public class SpringIntegrationChannelExample {
    private MessageChannel out;
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        SpringIntegrationChannelExample springIntExample = (SpringIntegrationChannelExample) context.getBean("springIntChannelExample");
    public void postEmployeeMsg() {
        Employee emp = new Employee(1, "Joe", 37);
        Message msg = MessageBuilder.withPayload(emp).build();

    public void setOut(MessageChannel out) {
        this.out = out;


Employee Employee: [Joe, ID: 1, AGE 37] processed

Download the Source Code

In this article, I have shown how to manually create a message and publish to an outbound channel.
You can download the source code here springintegrationchannel.zip


Leave A Reply