Archive for the ‘apache’ Category

synchronous JMS messaging with Spring …

Saturday, November 10th, 2007

My current work involves messaging and I have been working with Spring and JMS (ActiveMQ), which is a great combination, especially since you can make Message Driven Pojos (MDP).

In most cases I try to make interactions between components asynchronous, where a request is sent and a response is not immediately expected, but dealt with later, possibly by another component. However, there are situations where this isn’t desired and you need to be able to send a request and wait for the response before continuing.

Spring provides an excellent facility for this with it’s JMS remoting, but in my case I couldn’t use this because non-spring based components need to send requests. I didn’t find a way to achieve this out of the box with Spring so I had to write something to do the job. This is the code:

public Object convertAndSendAndReceiveAndConvert(Object request) {
    final String uuid = UUID.randomUUID().toString();
    convertAndSend(request, new MessagePostProcessor() {
        public Message postProcessMessage(Message message) throws JMSException {
            message.setJMSCorrelationID(uuid);
            return message;
        }
    });
    return receiveSelectedAndConvert(getReplyFromDestination(),
             "JMSCorrelationID='" + uuid + "'");
}

I put this code into my own subclass of JmsTemplate so the calling code can just call convertAndSendAndReceiveAndConvert(). I also added some properties to the JmsTemplate subclass to define the reply queue. The Spring definition of the template looks like this:
(I’m wrestling with my blog editor and have removed some XML characters, but I’m sure you get the gist.)

bean id="doSomethingSynchronousJmsTemplate" class="MyJmsTemplate"
    property name="connectionFactory" ref="connectionFactory"
    property name="defaultDestination" ref="requestQueue"
    property name="replyFromDestination" ref="replyQueue"
    property name="messageConverter" ref="xmlMessageConverter"
    property name="receiveTimeout" value="5"

I’m using a queue for requests and a queue for responses which “feels” right when considered against a temporary queue for this situation.
The calling code is much the same as other JmsTemplate usages:

    return (reply) _synchronousJmsTemplate
                     .convertAndSendAndReceiveAndConvert(request);

I’m converting the Request and Response objects using XStream in a custom MessageConverter subclass. This allows the receiving components to accept XML requests from any sender. As long as they conform the the XML wire format.

All is working nicely, and I hope this post helps you.

Obscene Lucene

Tuesday, February 13th, 2007

Today I tried Apache Lucene and when I got it running it was very fast and rather nice. What is obscene is my title for the entry as there isn’t anything obscene about Lucene but there was a annoyance in trying to get it running. I don’t think I should have had to go through this. Maybe I need more patience.

I downloaded version 2.0.0 and followed the instructions and ran the web app only to get a result like this:

org.apache.jasper.JasperException: Unable to compile class for JSP …
An error occurred at line: 60 in the jsp file: /results.jsp
Note: /home/jamesl/applications/jakarta-tomcat-5.0.28/work/
Catalina/localhost/luceneweb/org/apache/jsp/results_jsp.java uses or
overrides a deprecated API.

So I look for posts about the problem and I find I have to edit the results.jsp and change the following lines from:

try {
query = QueryParser.parse(queryString, "contents", analyzer); //parse the
} catch (ParseException e) {

To:

try {
QueryParser qp = new QueryParser("contents", analyzer);
query = qp.parse(queryString);
} catch (ParseException e) {

Then I restart Tomcat and I try again, but this time Lucene can’t find my index files. So off I go and read the instructions again but no matter how many times I read I can’t seem to overcome the ambiguity. I’m willing to concede that I’m probably just not getting these instructions. I tried removing the leading “/” and it all made sense to me when I got the next error. So I put the index files into the Tomcat “/work” folder and changed the path in the configuration.jsp to be “{path-to-tomcat}/work/index” and all was well and I was able to run the web example. Boy were they fast.

Maybe I’m too impatient or maybe I am spoilt coming from an Agile XP environment where the code works out of the box. Do you think it should work right out of the box? Would you give a toy that required batteries and not give batteries as well?

Minor Mina Irritation

Monday, February 12th, 2007

I’m looking/working through the Apache Mina examples and project to see if it’s a tool that will do a job for me. So I downloaded the project, extracted it and ran through the suggested Maven2 targets suggested in the Mina developer guide. No problems there as Maven2 did it’s thing with no pain what-so-ever. The I fired up eclipse and created a new project and imported Mina which was easy. However, there were lots of compile errors because the concurrent backport classes could not be found, amongst other things. This must be downloaded from a separate place (this requirement is listed in the FAQ). Then you need to modify the Mina Eclipse project to reference the backport-util-concurrent.jar, which is all fairly simple but the irritation doesn’t stop there. There are still unresolved references to JUnit’s assert (3.8.1 is referenced in the pom.xml but I downloaded 3.8.2) and other compile problems to be fixed. I think this time it’s the SpringFramework jars that I need.

So Mina can’t and probably shouldn’t ship these associated jars but there could be a comment on the Developer Guide saying that these jars are required and where to get them should you be using Eclipse. It’s a small irritation but one that I don’t think is mandatory for all to go through, especially those looking to use Mina for some task.

I’m still not up and running and admittedly I’ve only spent 15 minutes on the task, so when I get it running I’ll list the other requirements to get the Eclipse build working. I suspect it’s just the jars referenced above. All I wanted to do was run a few of the examples.