Spring: Handling incoming JMS messages asynchronously
Handling incoming JMS messages in an asynchronous manner requires, implementing a MessageListener, creating a connection, starting a thread, handling connection loss and thread death, etc…
Spring’s MessageListenerContainers provide a “Message Driven Pojo” framework that handles all that for you except implementing the MessageListener.
To take advantage of this you first need to create JMS connection factory and Destination:
<bean id="connectionPool" class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost"/> </bean> </property> </bean> <bean id="messageDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="MY.INCOMING" /> </bean>
Then implement the MessageListener interface:
@Component("IncomingMsgProcessor")
public class IncomingMsgProcessor implements MessageListener {
@Override
public void onMessage(Message message)
{
//Your message handling code here
}
}
And finally inject an instance into the MessageListenerContainer:
<bean id="incommingMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionPool" /> <property name="destination" ref="messageDestination" /> <property name="messageListener" ref="IncomingMsgProcessor" /> <!-- Retry connection every 10 seconds. --> <property name="recoveryInterval" value="10000" /> </bean>
org.springframework.jms.listener.DefaultMessageListenerContainer gives a nice set of features, but spring offers other implementations if more (or less) is needed. If your message listener needs access to the JMS session you can implement SessionAwareMessageListener which has a method that take the message and session onMessage(Message message, Session session) instead.
Spring: Injecting JAXB (Un)Marshaller
Create a JAXB context passing an array of root element classes:
<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance"> <constructor-arg> <list> <value type="java.lang.Class">my.example.rootclass.Root</value> </list> </constructor-arg> </bean>
Create prototypes for the marshallers/unmarshallers
<!-- Pool (un)marshallers to improve performance --> <bean id="marshallerTarget" class="javax.xml.bind.Marshaller" factory-bean="jaxbContext" factory-method="createMarshaller" scope="prototype"> </bean> <bean id="unmarshallerTarget" class="javax.xml.bind.Unmarshaller" factory-bean="jaxbContext" factory-method="createUnmarshaller" scope="prototype"> </bean>
Wrap these in pool targets:
<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource"> <property name="targetBeanName" value="marshallerTarget" /> <property name="maxSize" value="25" /> </bean> <bean id="unmarshallerPoolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource"> <property name="targetBeanName" value="unmarshallerTarget" /> <property name="maxSize" value="25" /> </bean>
And create pools for reusing marshallers/unmarshallers:
<bean id="marshaller" class="org.springframework.aop.framework.ProxyFactoryBean"> <qualifier value="marshaller" /> <property name="targetSource" ref="poolTargetSource" /> </bean> <bean id="unmarshaller" class="org.springframework.aop.framework.ProxyFactoryBean"> <qualifier value="unmarshaller" /> <property name="targetSource" ref="unmarshallerPoolTargetSource" /> </bean>
This will create one context that will be used to create up to 25 marshallers/unmarshallers. Each will be created on demand until there are 25 in use simultaneously, then calls to them will block until one becomes available. Be careful changing the state on the (un)marshallers. It would probably be a better idea to create prototypes for each configuration and inject a separate pool for each.
These can be injected in the application context or using annotations:
@Autowired
@Qualifier("unmarshaller")
private Unmarshaller unmarshaller;
@Autowired
@Qualifier("marshaller")
private Marshaller marshaller;
Spring: Injecting ActiveMQ JMS Connection
First create a connection factory.
<bean id="connectionPool" class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost"/> </bean> </property> </bean>
Create destiation:
<bean id="messageDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="MY.OUTGOING" /> </bean>
Inject this into spring a template:
<bean id="jmsSendTemplate" class="org.springframework.jms.core.JmsTemplate"> <qualifier value="messageTemplate" /> <property name="connectionFactory"> <ref bean="connectionFactory" /> </property> <property name="defaultDestination"> <ref bean="messageDestination" /> </property> </bean>
Which can then be injected in the application context or using annotations:
@Autowired
@Qualifier("messageTemplate")
JmsTemplate jmsReceiveTemplate;
Next Sequence Value Using Hibernate Entity Manager
To retrieve the next value of an Orcale sequence using the Hibernate Entity Manager first create a result set mapping.
@SqlResultSetMapping(name = "NextSequenceVal", columns = { @ColumnResult(name = "NEXTVAL") })
The mapping can go anywhere, I like to put it on the entity it is most closely associated with.
Then in your DAO.
public Long getNextElecTransUniqueId()
{
Query query = entityManager.createNativeQuery("SELECT MY_SEQUENCE.NEXTVAL from Dual",
"NextSequenceVal");
// Workaround for
// http://opensource.atlassian.com/projects/hibernate/browse/EJB-434
// which breaks query.getSingleResult()
return ((BigDecimal) query.getResultList().get(0)).longValue();
}
This example is written for Hibernate 3.3.2, Hibernate Entity Manager 3.4.0, Oracle 10g.
Note: in Entity Manager 3.4.0 if you use query.getSingleResult() you will get the exception:
Exception: org.hibernate.exception.SQLGrammarException: could not execute query^M
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)^M
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)^M
at org.hibernate.loader.Loader.doList(Loader.java:2235)^M
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)^M
at org.hibernate.loader.Loader.list(Loader.java:2124)^M
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:312)^M
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1723)^M
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)^M
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:175)^M
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:88)^M
Caused by: java.sql.SQLException: ORA-02287: sequence number not allowed here
Grails fails to start with cryptic error
I ran into a coding error in a Grails (1.0.4, this does not appear to be an issue in 1.1.1) app recently that proved very hard to diagnose. It turned out to be a simple coding error, just hard to spot syntax.
After a simple refactor by someone else I was asked to help figure out why the app would no longer run. We got a error like:
Failed startup of context org.mortbay.jetty.webapp.WebAppContext@59139826{/ClosureTest,/Users/ahahn/devel/src/test/ClosureTest/web-app}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass
at java.security.AccessController.doPrivileged(Native Method)
at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy:67)
at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy)
at Init_groovy$_run_closure6.doCall(Init_groovy:131)
at RunApp_groovy$_run_closure2.doCall(RunApp_groovy:66)
at RunApp_groovy$_run_closure2.doCall(RunApp_groovy)
at RunApp_groovy$_run_closure1.doCall(RunApp_groovy:57)
at RunApp_groovy$_run_closure1.doCall(RunApp_groovy)
at gant.Gant.dispatch(Gant.groovy:271)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.processTargets(Gant.groovy:436)
at gant.Gant.processArgs(Gant.groovy:372)
Caused by: java.lang.RuntimeException: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass
... 13 more
Caused by: java.lang.reflect.InvocationTargetException
... 13 more
Caused by: org.codehaus.groovy.grails.exceptions.NewInstanceCreationException: Could not create a new instance of class [TestController]!
... 13 more
Caused by: java.lang.VerifyError: (class: TestController$_closure2, method: doCall signature: (Ljava/lang/Object;)Ljava/lang/Object;) Incompatible argument to function
at TestController.(TestController.groovy)
... 13 more
2009-05-25 11:01:25.026::WARN: Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.RuntimeException: Unable to locate constructor with Class parameter for class org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass:
java.lang.VerifyError: (class: TestController$_closure2, method: doCall signature: (Ljava/lang/Object;)Ljava/lang/Object;) Incompatible argument to function
at TestController.(TestController.groovy)
at java.security.AccessController.doPrivileged(Native Method)
at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy:67)
at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy)
at Init_groovy$_run_closure6.doCall(Init_groovy:131)
at RunApp_groovy$_run_closure2.doCall(RunApp_groovy:66)
at RunApp_groovy$_run_closure2.doCall(RunApp_groovy)
at RunApp_groovy$_run_closure1.doCall(RunApp_groovy:57)
at RunApp_groovy$_run_closure1.doCall(RunApp_groovy)
at gant.Gant.dispatch(Gant.groovy:271)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.processTargets(Gant.groovy:436)
at gant.Gant.processArgs(Gant.groovy:372)
Which I could trace to the TestController code:
def list = Test.list( params )
def testMaps = []
if(list) {
list.each {test ->
if(test.bool) {
testMaps << [
foo: it.foo,
bar: "Bar ${it.bar}"
]
}
}
}
Spot it yet? No really, spot it yet? It is simply a case of an incomplete refactor, the each closure was changed to have a named parameter ‘test’ but not all code in the body was refactored. Most of the references to the current object in the iteration still used the default parameter ‘it’. So what you would expect to be a run time error actual blows up at start up.
Just in case, here it the working code:
def list = Test.list( params )
def testMaps = []
if(list) {
list.each {test ->
if(test.bool) {
testMaps << [
foo: test.foo,
bar: "Bar ${test.bar}"
]
}
}
}
What jar is that friggin class in?!?!?!
It seems to me that one of the most common tasks I have to preform when packaging a J2EE project is to find out what jar a class I depend on is in.
I have written (multiple times now) a simple perl script that finds all the jars in a given directory and scans them for a file. Thanks to the power of regular expressions you can pass in either com.foo.bar.Class or com/foo/bar/Class which ever is easier to cut-n-paste from the java.lang.NoClassDefFoundError, or similar, stack trace.
#!/usr/bin/perl
$JAR_COMMAND = "jar";
#Uncomment to use fastjar instead
#$JAR_COMMAND = "fastjar";
$class = "";
$lookin = ".";
if(@ARGV < 1) {
print "USAGE: \n\tclassfinder [dir to search fo jars]
<packageName>\n";
exit;
} elsif (@ARGV > 1) {
$lookin = $ARGV[0];
$class = $ARGV[1];
} else {
$class = $ARGV[0];
}
foreach $jar (`find $lookin -name '*\.jar'`) {
chomp $jar;
foreach $file (`$JAR_COMMAND -tf $jar`) {
chomp $file;
if($file =~ $class) {
print "$jar:$file\n"}
}
}
The script relies on the standard unix utility find, it works on unix, linux, osx and cygwin. If you have an easy way to do this without using find I’d love to see it.
Adventures in Grails – Running more than one version of your app
Update: Posted a patch to the Grails JIRA http://jira.codehaus.org/browse/GRAILS-2771
Grails provides a simple versioning system for your application with the app.version property in application.properties. When you build a .war for deployment the file name is given by "${app.name}-${app.version}.war". For me this is great, I frequently need to run multiple versions of an app to support multiple client development branches. However, when I dropped myApp-0.2.war into tomcat’s webapp dir along side myApp-0.1.war the deployment failed. On tomcat 6.0.16 the error is cryptic and far from helpful SEVERE: Error listenerStart. Tomcat 5.5.26 provided a much more useful error message. It turns out that Grails only includes the version number in the .war file name and not in web.xml where it uses only the app name.
To fix this all you need to do is edit the web.xml of one of the versions and replace the app name with something else. Well, thats not very Groovy! To have Grails do this for you it is a simple change to the Package.groovy script that ships with Grails.
The lines (264-266):
Ant.copy(file:"${grailsHome}/src/war/WEB-INF/web${servletVersion}.template.xml", tofile:tmpWebXml)
Ant.replace(file:tmpWebXml, token:"@grails.project.key@", value:"${grailsAppName}")
get changed to:
Ant.copy(file:"${grailsHome}/src/war/WEB-INF/web${servletVersion}.template.xml", tofile:tmpWebXml, overwrite:true)
Ant.replace(file:tmpWebXml, token:"@grails.project.key@", value:"${grailsAppName}-${grailsAppVersion}")
The overwrite:true in the copy task is necessary so that when the version is changed, web.xml is updated. This will break you if you rely on hand made changes to web.tmp.xml if your ~/.grails directory. Though, you might not want to do things that way anyway.On a side note, you may want to define more environments in grails-app/config/DataSources.groovy if you have changed your app’s DB schema to prevent errors.
MySQL install fails on Ubuntu with static IP
Update:
It was not the static IP address, but the fact that due to a typo in /etc/network/interfaces the loopback device was not active.
Installing MySQL server on Ubuntu fails if the machine has a static IP address.
sudo apt-get install mysql-server
Results in:
Setting up mysql-server-5.0 (5.0.45-1ubuntu3.1) ...
* Stopping MySQL database server mysqld
...done.
* Starting MySQL database server mysqld
...fail!
invoke-rc.d: initscript mysql, action "start" failed.
dpkg: error processing mysql-server-5.0 (--configure):
subprocess post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of mysql-server:
mysql-server depends on mysql-server-5.0; however:
Package mysql-server-5.0 is not configured yet.
dpkg: error processing mysql-server (--configure):
dependency problems - leaving unconfigured
Processing triggers for libc6 ...
ldconfig deferred processing now taking place
Errors were encountered while processing:
mysql-server-5.0
mysql-server
E: Sub-process /usr/bin/dpkg returned an error code (1)
To fix this edit /etc/mysql/my.conf and change the bind-address to the static address of the machine. Then rerun apt-get to finish the configuration of the packages.
sudo apt-get -f install
Reported to Ubuntu launchpad
Adventures in Grails – Debugging
Update :
It was pointed out to me on the Grails user list that the ‘grails-debug‘ command can be used to do what I posted below. However, the code below is slightly more flexible in that you can control the port, transport and suspend flag with environmental variables.
Working with Grails so far has been great. However, there is no easy way to (that I could find) to fire up Grails and attach a debugger. So I made a few changes to the startup script $GRAILS_HOME/bin/startGrails to handle a debug argument. This is *NIX systems only (including OS X), I haven’t worked with batch files in so long (and have no need for it) that I did not make the changes to $GRAILS_HOME/bin/startGrails.bat. Perhaps someone that uses that other OS to develop on could port the changes.
The script uses the environmental variables:
- JPDA_ADDRESS #the port to listen on, default 8000
- JPDA_TRANSPORT #transport to use, default ‘dt_socket’
- JPDA_WAIT #if ‘y’ then wait for the debugger to attach, default ‘n’
If anyone of these is not set it’s default value is used.
To start the VM for debugging add ‘debug’ as the first arg to the grails command.
grails debug run-app
The changes are in two places. First:
if [ "$1" = "-cp" ] || [ "$1" = "-classpath" ]; then CP=$2 shift 2 fi ARGUMENTS=$@
becomes
#Test for and setup debug option
if [ ! $JPDA_ADDRESS ]
then
JPDA_ADDRESS=8000
fi
if [ ! $JPDA_TRANSPORT ]
then
JPDA_TRANSPORT="dt_socket"
fi
if [ ! $JPDA_WAIT ]
then
JPDA_WAIT="n"
fi
if [ $# -ne 0 ]
then
if [ $1 == "debug" ]
then
shift
GRAILS_DEBUG_OPTS="-Xdebug -Xrunjdwp:transport=${JPDA_TRANSPORT},address=${JPDA_ADDRESS},server=y,suspend=${JPDA_WAIT}"
fi
fi
if [ "$1" = "-cp" ] || [ "$1" = "-classpath" ]; then
CP=$2
shift 2
fi
ARGUMENTS=$@
and in the startGrails function:
startGrails() {
CLASS=$1
shift
JAVA_OPTS="-server -Xmx512M $JAVA_OPTS"
# Start the Profiler or the JVM
if $useprofiler; then
runProfiler
else
if [ $# -eq 0 ] ; then # no argument given
exec "$JAVACMD" $JAVA_OPTS \
-classpath "$STARTER_CLASSPATH" \
-Dprogram.name="$PROGNAME" \
-Dgroovy.starter.conf="$GROOVY_CONF" \
-Dgrails.home="$GRAILS_HOME" \
-Dbase.dir="." \
-Dtools.jar="$TOOLS_JAR" \
$STARTER_MAIN_CLASS \
--main $CLASS \
--conf "$GROOVY_CONF" \
--classpath "$CP"
else
exec "$JAVACMD" $JAVA_OPTS \
-classpath "$STARTER_CLASSPATH" \
-Dprogram.name="$PROGNAME" \
-Dgroovy.starter.conf="$GROOVY_CONF" \
-Dgrails.home="$GRAILS_HOME" \
-Dbase.dir="." \
-Dtools.jar="$TOOLS_JAR" \
$STARTER_MAIN_CLASS \
--main $CLASS \
--conf "$GROOVY_CONF" \
--classpath "$CP" \
"${ARGUMENTS}"
fi
fi
}
becomes
startGrails() {
CLASS=$1
shift
JAVA_OPTS="-server -Xmx512M $JAVA_OPTS"
# Start the Profiler or the JVM
if $useprofiler; then
runProfiler
else
if [ $# -eq 0 ] ; then # no argument given
exec "$JAVACMD" $JAVA_OPTS \
-classpath "$STARTER_CLASSPATH" \
"$GRAILS_DEBUG_OPTS" \
-Dprogram.name="$PROGNAME" \
-Dgroovy.starter.conf="$GROOVY_CONF" \
-Dgrails.home="$GRAILS_HOME" \
-Dbase.dir="." \
-Dtools.jar="$TOOLS_JAR" \
$STARTER_MAIN_CLASS \
--main $CLASS \
--conf "$GROOVY_CONF" \
--classpath "$CP"
else
exec "$JAVACMD" $JAVA_OPTS \
-classpath "$STARTER_CLASSPATH" \
"$GRAILS_DEBUG_OPTS" \
-Dprogram.name="$PROGNAME" \
-Dgroovy.starter.conf="$GROOVY_CONF" \
-Dgrails.home="$GRAILS_HOME" \
-Dbase.dir="." \
-Dtools.jar="$TOOLS_JAR" \
$STARTER_MAIN_CLASS \
--main $CLASS \
--conf "$GROOVY_CONF" \
--classpath "$CP" \
"${ARGUMENTS}"
fi
fi
}