PDA

View Full Version : Combining integration and AMQP



SimonP
Nov 10th, 2010, 11:44 AM
I am trying to create a simple spike that is an extension to one of the spring integration samples for transferring files.

In this original example the config looks like this


<bean class="org.springframework.beans.factory.config.PropertyP laceholderConfigurer"/>

<file:inbound-channel-adapter id="filesIn"
directory="file:${java.io.tmpdir}/spring-integration-samples/input"
> <!--filename-pattern="[a-z]+.txt"-->
<integration:poller id="poller" fixed-delay="5000"/>
</file:inbound-channel-adapter>

<file:file-to-string-transformer input-channel="filesIn" output-channel="strings"/>

<integration:channel id="strings"/>

<integration:service-activator input-channel="strings"
output-channel="filesOut"
ref="handler"/>

<file:outbound-channel-adapter id="filesOut" directory="file:${java.io.tmpdir}/spring-integration-samples/output"/>

<bean id="handler" class="org.springframework.integration.samples.filecopy.H andler"/>


I have changed this to


<bean class="org.springframework.beans.factory.config.PropertyP laceholderConfigurer"/>
<bean class="org.springframework.integration.aop.PublisherAnnot ationBeanPostProcessor"/>
<integration:annotation-config/>
<file:inbound-channel-adapter id="filesIn"
directory="file:${java.io.tmpdir}/spring-integration-samples/input"
> <!--filename-pattern="[a-z]+.txt"-->
<integration:poller id="poller" fixed-delay="5000"/>
</file:inbound-channel-adapter>

<file:file-to-string-transformer input-channel="filesIn" output-channel="strings"/>

<integration:channel id="strings"/>

<!-- the handler in this case is responsible for sending messages to a real Q -->
<integration:service-activator input-channel="strings" ref="handler"/> <!--output-channel="filesOut"-->
<bean id="handler" class="com.musa.spike.Handler"/>

<!-- this should be called when the FileMessageHandler gets Fired -->
<file:outbound-channel-adapter id="filesOut" directory="file:${java.io.tmpdir}/spring-integration-samples/output"/>

The handler used to just log and return the same string.

Now it does this


public void handleString(String input) {System.out.println("Sending file contents to teh default Rabbit MQ Queue");
ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfi guration.class);
AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
amqpTemplate.convertAndSend(input);}


In a separate process I have this target setup as a message listener



public class FileMessageHandler {

@Publisher(channel="filesOut")
public String handleMessage(String text) {
System.out.println("Received: message from Rabbit MQ");
return text;
}

}


This gets called as expected but the output-channel-adapter is not being invoked.


<file:outbound-channel-adapter id="filesOut" directory="file:${java.io.tmpdir}/spring-integration-samples/output"/>

I have added


<bean class="org.springframework.integration.aop.PublisherAnnot ationBeanPostProcessor"/>
<integration:annotation-config/>


To the integration xml file. Can anyone tell me what I may have missed?

Dave Syer
Nov 29th, 2010, 05:44 AM
Your handler has a void return type, so there is nothing to forward to the output channel.

N.B. there is a spring-integration-amqp adapter module in the Spring Integration source (not released with 2.0 because Spring AMQP is not ready yet). You could have used that to send the message through a gateway, which would have been more idiomatic in integration terms.

SimonP
Dec 1st, 2010, 03:44 AM
Your handler has a void return type, so there is nothing to forward to the output channel.

This is deliberate as the message at this point gets passed to RabbitMQ it does not go to an output channel. I have managed to complete the process and have posted the result here (http://forum.springsource.org/showthread.php?t=98257)


N.B. there is a spring-integration-amqp adapter module in the Spring Integration source (not released with 2.0 because Spring AMQP is not ready yet). You could have used that to send the message through a gateway, which would have been more idiomatic in integration terms.

I did look at this but I was not sure it was fully functional in the sandbox. There was no POM so I could not resolve dependencies and build the sandbox source. I agree that your suggestion would be the desired approach and would be far more consistent with the idioms of the rest of the framework.

Thanks

Simon