יום שלישי, 14 במאי 2013

Spring integration SubscribableChannel

In order to convert the Spring integration Hello world demo to Async I changed the output  channel declaration in the spring xml to publish-subscribe-channel  :

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             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">

    <channel id="inputChannel"/>

    <publish-subscribe-channel id="outputChannel">
    </publish-subscribe-channel>

    <service-activator input-channel="inputChannel"
                       output-channel="outputChannel"
                       ref="helloService"
                       method="sayHello"/>

    <beans:bean id="helloService" class="org.springframework.integration.samples.helloworld.HelloService"/>

</beans:beans>

Capture5 

using the following code :

public static void main(String[] args) {
AbstractApplicationContext context = new
ClassPathXmlApplicationContext("/META-INF/spring/integration/helloWorldDemo.xml",
HelloWorldApp.class);
MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
SubscribableChannel outputChannel = context.getBean("outputChannel", SubscribableChannel.class);

outputChannel.subscribe(new MessageHandler()
{
@Override
public void handleMessage(Message<?> msg)
{
logger.info("==> HelloWorldDemo Async message: " + msg.getPayload());
}
});

for ( int i= 1 ; i < 3 ; i ++)
{
logger.info("Sending no:" + Integer.toString (i));
inputChannel.send(new GenericMessage<String>("Mr no" + Integer.toString (i)));
}

I changed the pollable output channel to SubscribableChannel and declared and event handler for the channel messages using the subscribe method


and the result :


09:39:58.400 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] Sending no:1
09:40:03.585 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] ==> HelloWorldDemo Async message: Hello Mr no1
09:40:12.248 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] Sending no:2
09:40:17.719 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] ==> HelloWorldDemo Async message: Hello Mr no2


Note that an unsubscribe method is although available used to  unsubscribe  the channel for listening to events .


There is an option to multi subscribers to listen to the same output channel

outputChannel.subscribe(new MessageHandler()
{
@Override
public void handleMessage(Message<?> msg)
{
logger.info("==> HelloWorldDemo Async message: " + msg.getPayload());
}
});

outputChannel.subscribe(new MessageHandler()
{
@Override
public void handleMessage(Message<?> msg)
{
logger.info("==> HelloWorldDemo Async message (2): " + msg.getPayload());
}
});



The result :


09:56:27.197 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] Sending no:1
09:56:27.267 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] ==> HelloWorldDemo Async message: Hello Mr no1
09:56:27.267 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] ==> HelloWorldDemo Async message (2): Hello Mr no1
09:56:27.267 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] Sending no:2
09:56:27.267 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] ==> HelloWorldDemo Async message: Hello Mr no2
09:56:27.267 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] ==> HelloWorldDemo Async message (2): Hello Mr no2

אין תגובות:

הוסף רשומת תגובה