This example does actually what you are doing:
public void onMessage(Message inMessage)
{
try
{
System.out.println("Message received: " + inMessage);
InitialContext ctx = new InitialContext();
ConnectionFactory cf = (ConnectionFactory) ctx.lookup("java:comp/env/CF");
Connection connection = cf.createConnection();
Destination dest = (Destination) ctx.lookup("java:comp/env/testqueue2");
Session session = connection.createSession(true, 0);
MessageProducer producer = session.createProducer(dest);
TextMessage msg = session.createTextMessage("FWD: " + ((TextMessage) inMessage).getText());
producer.send(msg);
producer.close();
session.close();
connection.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
However, since it is an MDB, the number of MDB instances actually decides about the message sequence in the final destination. If the number of instances is 1, I would expect to have the same sequence in the final destination as they were send to the MDB destination.
On the other hand - it is up to the JTA transaction manager when it runs the 2PC on a transaction. It may do it FIFO or LIFO or in parallel. The JEE spec states that messages which should be in sequence should be send in a single transaction.
But what I don't understand is why some of your messages are not sent within a XA transaction at all. This seems to be strange and you should actually see errors in Websphere's log files.