Apache ActiveMQ is the bad-boy of all the JMS servers out there. Firstly it is ‘free’. Secondly it is very good. For those who think free does not go with good (like I did) – well, lets just say life got a bit sweeter for them.
ActiveMQ provides built-in failover handling. Failover handling is very important for any kind of JMS application. Failover handling means deciding what to do when something bad happens which is outside your control.
For example your application has subscribed to a Topic but the JMS server drops the connection or the network link goes down. Your application is left with a JMS error and a dead connection to deal with.
There are several things you might want to do in that case, such as:
– Try reconnecting after some time and keep on trying till you can reconnect
– Switch over to another JMS server (if present)
We might also need to configure things like how soon to reconnect, which URL to treat as primary (in case of switching).
As ActiveMQ provides this functionality ‘out-of-the-box’ it makes life easier. The way to implement this is very simple as well. We just add the failover settings to the Naming URI in the JMS Connection settings.
A normal JMS Naming URI looks like: tcp://hostname:port (e.g. tcp://localhost:6666)
If we wanted to use the ActiveMQ specific failover we change the JMS Naming URI as:
For a single server auto-reconnect –
failover://(tcp://hostname:port) or failover:(tcp://hostname:port)
*Try both the versions (with and without the //) and see what works in your specific case.
In this case if the connection goes down for some external reason, ActiveMQ will try and reconnect.
For a backup-server auto-switching –
*Again try both the versions (with and without the //) and see what works in your specific case.
In this case if the primary connections goes down then ActiveMQ will try and switch to the secondary. In case both servers are active (e.g. for load-balancing) we may want to choose randomly between the two or in case of primary-secondary we may want start with primary first and keep the secondary for failover. We can do this by using the ‘randomize’ option:
Here randomize=false means the primary URI will be tried first.
Check the URL in the reference section for more configurations.
[Further Reference: http://activemq.apache.org/failover-transport-reference.html]
javax.jms.JMSException: Failed to build body from bytes. Reason: java.io.StreamCorruptedException: invalid type code: 09
If you see this error in your application when trying to call the getObject method on an ‘Object Message’ it means something has gone wrong when you tried to de-serialize the object at your end.
This error is thrown when the Java IO library does not recognize the type code its found in the object data.
This is most often caused by mismatched ActiveMQ libraries between the sender (which serializes the object) and receiver (which de-serializes it).
If you have no control on the sending application then its time to hold your head in your hands and cry. You just learned a valuable lesson – if you are using JMS to decouple your applications then ALSO use a neutral message format (like XML) instead of serialized objects.