Search in sources :

Example 26 with QueueSender

use of javax.jms.QueueSender in project activemq-artemis by apache.

the class SimpleOpenWireTest method testTempQueueDelete.

@Test
public void testTempQueueDelete() throws Exception {
    connection.start();
    QueueSession queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue tempQueue = queueSession.createTemporaryQueue();
    ActiveMQConnection newConn = (ActiveMQConnection) factory.createConnection();
    try {
        QueueSession newQueueSession = newConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueSender queueSender = newQueueSession.createSender(tempQueue);
        Message msg = queueSession.createMessage();
        queueSender.send(msg);
        try {
            QueueReceiver consumer = newQueueSession.createReceiver(tempQueue);
            fail("should have gotten exception but got consumer: " + consumer);
        } catch (JMSException ex) {
        // correct
        }
        connection.close();
        try {
            Message newMsg = newQueueSession.createMessage();
            queueSender.send(newMsg);
        } catch (JMSException e) {
        // ok
        }
    } finally {
        newConn.close();
    }
}
Also used : MapMessage(javax.jms.MapMessage) StreamMessage(javax.jms.StreamMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) TemporaryQueue(javax.jms.TemporaryQueue) JMSException(javax.jms.JMSException) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 27 with QueueSender

use of javax.jms.QueueSender in project OpenOLAT by OpenOLAT.

the class JmsIndexer method indexDocument.

@Override
public void indexDocument(String type, Long key) {
    QueueSender sender;
    QueueSession session;
    try {
        JmsIndexWork workUnit = new JmsIndexWork(JmsIndexWork.INDEX, type, key);
        session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        ObjectMessage message = session.createObjectMessage();
        message.setObject(workUnit);
        sender = session.createSender(getJmsQueue());
        sender.send(message);
        session.close();
    } catch (JMSException e) {
        log.error("", e);
    }
}
Also used : QueueSender(javax.jms.QueueSender) ObjectMessage(javax.jms.ObjectMessage) JMSException(javax.jms.JMSException) QueueSession(javax.jms.QueueSession)

Example 28 with QueueSender

use of javax.jms.QueueSender 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)

Example 29 with QueueSender

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

the class JmsServiceEngine method runQueue.

protected Map<String, Object> runQueue(ModelService modelService, Map<String, Object> context, Server server) throws GenericServiceException {
    String serverName = server.getJndiServerName();
    String jndiName = server.getJndiName();
    String queueName = server.getTopicQueue();
    String userName = server.getUsername();
    String password = server.getPassword();
    String clientId = server.getClientId();
    InitialContext jndi = null;
    QueueConnectionFactory factory = null;
    QueueConnection con = null;
    try {
        jndi = JNDIContextFactory.getInitialContext(serverName);
        factory = (QueueConnectionFactory) 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 = (QueueConnectionFactory) jndi.lookup(jndiName);
        } catch (GeneralException ge2) {
            throw new GenericServiceException("Problems getting JNDI InitialContext.", ge2.getNested());
        } catch (NamingException ne2) {
            throw new GenericServiceException("JNDI lookup problem.", ne2);
        }
    }
    try {
        con = factory.createQueueConnection(userName, password);
        if (clientId != null && clientId.length() > 1)
            con.setClientID(clientId);
        con.start();
        QueueSession session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = (Queue) jndi.lookup(queueName);
        QueueSender sender = session.createSender(queue);
        // create/send the message
        Message message = makeMessage(session, modelService, context);
        sender.send(message);
        if (Debug.verboseOn())
            Debug.logVerbose("Sent JMS Message to " + queueName, module);
        // close the connections
        sender.close();
        session.close();
        con.close();
    } 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 : XAQueueConnection(javax.jms.XAQueueConnection) QueueConnection(javax.jms.QueueConnection) GeneralException(org.apache.ofbiz.base.util.GeneralException) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) XAQueueConnectionFactory(javax.jms.XAQueueConnectionFactory) QueueConnectionFactory(javax.jms.QueueConnectionFactory) QueueSender(javax.jms.QueueSender) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) XAQueueSession(javax.jms.XAQueueSession) QueueSession(javax.jms.QueueSession)

Example 30 with QueueSender

use of javax.jms.QueueSender in project core by weld.

the class EJBTest method testMdbUsable.

@Test
public void testMdbUsable(Control control) throws Exception {
    InitialContext ctx = new InitialContext();
    QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
    QueueConnection connection = factory.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = (Queue) ctx.lookup(WildFly8EEResourceManager.TEST_QUEUE_DESTINATION);
    QueueSender sender = session.createSender(queue);
    sender.send(session.createTextMessage(MESSAGE));
    control.getLatch().await();
    Assert.assertTrue(control.isMessageDelivered());
    Assert.assertTrue(control.isContextSet());
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueConnectionFactory(javax.jms.QueueConnectionFactory) QueueSender(javax.jms.QueueSender) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Aggregations

QueueSender (javax.jms.QueueSender)59 QueueSession (javax.jms.QueueSession)54 Queue (javax.jms.Queue)36 TextMessage (javax.jms.TextMessage)28 QueueReceiver (javax.jms.QueueReceiver)25 JMSException (javax.jms.JMSException)14 Message (javax.jms.Message)14 QueueConnection (javax.jms.QueueConnection)14 Test (org.junit.jupiter.api.Test)14 Serializable (java.io.Serializable)11 ObjectMessage (javax.jms.ObjectMessage)10 Test (org.junit.Test)8 RMQDestination (com.rabbitmq.jms.admin.RMQDestination)5 InitialContext (javax.naming.InitialContext)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 MapMessage (javax.jms.MapMessage)4 QueueConnectionFactory (javax.jms.QueueConnectionFactory)4 IFSAQueue (com.ing.ifsa.IFSAQueue)3 GetResponse (com.rabbitmq.client.GetResponse)3