Search in sources :

Example 1 with XAQueueSession

use of javax.jms.XAQueueSession in project ofbiz-framework by apache.

the class JmsServiceEngine method runXaQueue.

protected Map<String, Object> runXaQueue(ModelService modelService, Map<String, Object> context, Element server) throws GenericServiceException {
    String serverName = server.getAttribute("jndi-server-name");
    String jndiName = server.getAttribute("jndi-name");
    String queueName = server.getAttribute("topic-queue");
    String userName = server.getAttribute("username");
    String password = server.getAttribute("password");
    String clientId = server.getAttribute("client-id");
    InitialContext jndi = null;
    XAQueueConnectionFactory factory = null;
    XAQueueConnection con = null;
    try {
        jndi = JNDIContextFactory.getInitialContext(serverName);
        factory = (XAQueueConnectionFactory) jndi.lookup(jndiName);
    } catch (GeneralException ge) {
        throw new GenericServiceException("Problems getting JNDI InitialContext.", ge.getNested());
    } catch (NamingException ne) {
        JNDIContextFactory.clearInitialContext(serverName);
        try {
            jndi = JNDIContextFactory.getInitialContext(serverName);
            factory = (XAQueueConnectionFactory) jndi.lookup(jndiName);
        } catch (GeneralException ge2) {
            throw new GenericServiceException("Problems getting JNDI InitialContext.", ge2.getNested());
        } catch (NamingException ne2) {
            throw new GenericServiceException("JNDI lookup problems.", ne2);
        }
    }
    try {
        con = factory.createXAQueueConnection(userName, password);
        if (clientId.length() > 1)
            con.setClientID(userName);
        con.start();
        // enlist the XAResource
        XAQueueSession session = con.createXAQueueSession();
        XAResource resource = session.getXAResource();
        if (TransactionUtil.getStatus() == TransactionUtil.STATUS_ACTIVE)
            TransactionUtil.enlistResource(resource);
        Queue queue = (Queue) jndi.lookup(queueName);
        QueueSession qSession = session.getQueueSession();
        QueueSender sender = qSession.createSender(queue);
        // create/send the message
        Message message = makeMessage(session, modelService, context);
        sender.send(message);
        if (TransactionUtil.getStatus() != TransactionUtil.STATUS_ACTIVE)
            session.commit();
        Debug.logInfo("Message sent.", module);
        // close the connections
        sender.close();
        session.close();
        con.close();
    } catch (GenericTransactionException gte) {
        throw new GenericServiceException("Problems enlisting resource w/ transaction manager.", gte.getNested());
    } catch (NamingException ne) {
        throw new GenericServiceException("Problems with JNDI lookup.", ne);
    } catch (JMSException je) {
        throw new GenericServiceException("JMS Internal Error.", je);
    }
    return ServiceUtil.returnSuccess();
}
Also used : GeneralException(org.apache.ofbiz.base.util.GeneralException) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) JMSException(javax.jms.JMSException) XAQueueConnection(javax.jms.XAQueueConnection) InitialContext(javax.naming.InitialContext) XAResource(javax.transaction.xa.XAResource) XAQueueSession(javax.jms.XAQueueSession) QueueSender(javax.jms.QueueSender) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) XAQueueConnectionFactory(javax.jms.XAQueueConnectionFactory) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) NamingException(javax.naming.NamingException) Queue(javax.jms.Queue) XAQueueSession(javax.jms.XAQueueSession) QueueSession(javax.jms.QueueSession)

Aggregations

JMSException (javax.jms.JMSException)1 MapMessage (javax.jms.MapMessage)1 Message (javax.jms.Message)1 Queue (javax.jms.Queue)1 QueueSender (javax.jms.QueueSender)1 QueueSession (javax.jms.QueueSession)1 XAQueueConnection (javax.jms.XAQueueConnection)1 XAQueueConnectionFactory (javax.jms.XAQueueConnectionFactory)1 XAQueueSession (javax.jms.XAQueueSession)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 XAResource (javax.transaction.xa.XAResource)1 GeneralException (org.apache.ofbiz.base.util.GeneralException)1 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)1 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)1