In my Java EE application I have implemented an asynchronous DB logger as an MDB which receives XML messages via JMS and writes them to the DB.
In another MDB I create a log message and send them to the input queue of the logger MDB using the following code:
public static void log(String correlId, String message, String data) throws Exception{
SysLogEntry sysLogEntry = new SysLogEntry();
sysLogEntry.setCorrelId(correlId);
sysLogEntry.setDatetimeCreate(new Date());
sysLogEntry.setMessage(message);
sysLogEntry.setData(data);
ConnectionFactory jmsConnectionFactory = (ConnectionFactory)initialContext.lookup(JMS_CONNECTION_FACTORY_JNDI_NAME);
Destination logEventDestination = (Destination) initialContext.lookup(LOG_EVENT_DESTINATION_JNDI_NAME);
JmsUtils.sendMsgToDestination(JaxbUtils.toString(sysLogEntry, jaxbContext), jmsConnectionFactory, logEventDestination, false, Session.AUTO_ACKNOWLEDGE);
}
public static void sendMsgToDestination(String payload, ConnectionFactory connFactory, Destination destination, boolean sessionTransacted, int acknowledgeMode) throws JMSException{
if(payload == null)
throw new IllegalArgumentException("Message payload is null");
if(connFactory == null)
throw new IllegalArgumentException("Connection factory is null");
if(destination == null)
throw new IllegalArgumentException("Message destination is null");
Connection connection = null;
try{
connection = connFactory.createConnection();
Session session = connection.createSession(sessionTransacted, acknowledgeMode);
MessageProducer messageProducer = session.createProducer(destination);
TextMessage textMessage = session.createTextMessage();
textMessage.setText(payload);
messageProducer.send(textMessage);
} finally {
if(connection != null){
try{
connection.close();
} catch (JMSException ignore){
}
}
}
}
where
SysLogEntry
is a class with JAXB annotations, which I use for serializationJMS_CONNECTION_FACTORY_JNDI_NAME
is the JNDI name of an XA connection factory
However every time the transaction in which I create the log message is rolled back, the log message is not put to the loggers input queue.
Can somebody tell me what is wrong with my code? I have considered to use a separate stateless session bean which would start a new transaction for log message producing, but there is a disadvantage with this approach: inside an EJB I could easily let the container inject the logger bean, but I have also non EJB objects in which I would like to do asynchronous logging.
My application servier is Weblogic 10.3.3