יום שני, 13 במאי 2013

About spring integration Hello world sample

The Hello world demo construct a simple  integration graph:

Capture4 

Where the message enter the input channel processed by the service activator and exit form the output Chanel .

The declaration in the spring integration descriptor xml:

<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"/>

    <channel id="outputChannel">
        <queue capacity="10"/>
    </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>

Note that the output channel is declared with a queue of 10 messages allowed to be stored before consuming .

The code for using the integration graph:

The pojo Hello service :

public class HelloService
{

public String sayHello(String name) {
return "Hello " + name;
}
}



The input and the output channels reference:


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





Lets try to send 14 messages behind the output channel capacity :


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


The application stack waiting for the output channel to be consumed:


08:35:01.874 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] Sending no:9
08:35:01.875 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] Sending no:10
08:35:01.875 INFO  [main][org.springframework.integration.samples.helloworld.HelloWorldApp] Sending no:11


Trying to receive the message in endless loop :


 
while (true)
{
Message theMessage = outputChannel.receive(0);
logger.info("==> HelloWorldDemo: " + theMessage.getPayload());
}



cause a java.lang.NullPointerException when the output channel queue gets empty .


In the next post I will consume the message async in order to void the stacking of the sender and the null exception in the receiver channel.

אין תגובות:

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