Search in sources :

Example 31 with QueueConnection

use of javax.jms.QueueConnection in project wildfly by wildfly.

the class GetCallerPrincipalTestCase method testMDBLifecycle.

/**
 * Run this one in the container so it can lookup the queue
 * @throws Exception
 */
@OperateOnDeployment("test")
@Test
public void testMDBLifecycle() throws Exception {
    deployer.deploy("mdb");
    final Callable<Void> callable = () -> {
        ITestResultsSingleton results = this.getResultsSingleton();
        MessageProducer producer = null;
        MessageConsumer consumer = null;
        QueueConnection conn = null;
        Session session = null;
        try {
            QueueConnectionFactory qcf = (QueueConnectionFactory) new InitialContext().lookup("java:/ConnectionFactory");
            Queue queue = (Queue) new InitialContext().lookup("java:jboss/" + QUEUE_NAME);
            conn = qcf.createQueueConnection("guest", "guest");
            conn.start();
            session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            TemporaryQueue replyQueue = session.createTemporaryQueue();
            TextMessage msg = session.createTextMessage("Hello world");
            msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
            msg.setJMSReplyTo(replyQueue);
            producer = session.createProducer(queue);
            producer.send(msg);
            consumer = session.createConsumer(replyQueue);
            Message replyMsg = consumer.receive(5000);
            Object obj = ((ObjectMessage) replyMsg).getObject();
            log.trace("MDB message get: " + obj);
            Assert.assertEquals(OK + "start", results.getMdb("postconstruct"));
            deployer.undeploy("mdb");
            Assert.assertEquals(OK + "stop", results.getMdb("predestroy"));
        } finally {
            if (consumer != null) {
                consumer.close();
            }
            if (producer != null) {
                producer.close();
            }
            if (session != null) {
                session.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
        return null;
    };
    Util.switchIdentitySCF("user1", "password1", callable);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueConnectionFactory(javax.jms.QueueConnectionFactory) InitialContext(javax.naming.InitialContext) QueueConnection(javax.jms.QueueConnection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) QueueSession(javax.jms.QueueSession) OperateOnDeployment(org.jboss.arquillian.container.test.api.OperateOnDeployment) Test(org.junit.Test)

Example 32 with QueueConnection

use of javax.jms.QueueConnection in project wildfly by wildfly.

the class HelloMDB method sendReply.

private void sendReply(String msg, Queue destination, String messageID) throws JMSException {
    QueueConnection conn = qFactory.createQueueConnection("guest", "guest");
    try {
        QueueSession session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        conn.start();
        QueueSender sender = session.createSender(destination);
        TextMessage message = session.createTextMessage(msg);
        message.setJMSCorrelationID(messageID);
        sender.send(message, DeliveryMode.NON_PERSISTENT, 4, 500);
    } finally {
        conn.close();
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueSender(javax.jms.QueueSender) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage)

Example 33 with QueueConnection

use of javax.jms.QueueConnection in project wildfly by wildfly.

the class MessageDrivenTimeoutTestCase method sendMessage.

static Queue sendMessage(String text, String queueJndi, InitialContext initCtx) throws Exception {
    QueueConnection connection = getConnection(initCtx);
    connection.start();
    Queue replyDestination = null;
    try {
        final QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        final Message message = session.createTextMessage(text);
        replyDestination = (Queue) initCtx.lookup(TransactionTimeoutQueueSetupTask.REPLY_QUEUE_JNDI_NAME);
        message.setJMSReplyTo(replyDestination);
        final Destination destination = (Destination) initCtx.lookup(queueJndi);
        final MessageProducer producer = session.createProducer(destination);
        producer.send(message);
        producer.close();
    } finally {
        connection.close();
    }
    return replyDestination;
}
Also used : Destination(javax.jms.Destination) QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Example 34 with QueueConnection

use of javax.jms.QueueConnection in project wildfly by wildfly.

the class MessageDrivenTimeoutTestCase method receiveMessage.

static String receiveMessage(Queue replyQueue, InitialContext initCtx, boolean isCommitExpected) throws Exception {
    QueueConnection connection = getConnection(initCtx);
    connection.start();
    try {
        final QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        final QueueReceiver receiver = session.createReceiver(replyQueue);
        // when expecting commit (message should be in queue): wait a bit longer before failing
        // when expecting rollback (message should not be in queue): fail faster only till timeout elapses
        final Message reply = receiver.receive(TimeoutUtil.adjust(isCommitExpected ? 5000 : TxTestUtil.timeoutWaitTime_ms));
        // waiting for synchro could be finished before checking
        Thread.sleep(TimeoutUtil.adjust(500));
        if (reply == null)
            return null;
        return ((TextMessage) reply).getText();
    } finally {
        connection.close();
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) QueueReceiver(javax.jms.QueueReceiver) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage)

Example 35 with QueueConnection

use of javax.jms.QueueConnection in project Payara by payara.

the class MessageBean method onMessage.

public void onMessage(Message message) {
    System.out.println("Got message!!!");
    QueueConnection connection = null;
    try {
        System.out.println("Calling hello1 stateless bean");
        hello1.hello("local ejb3.0 stateless");
        System.out.println("Calling hello2 stateful bean");
        hello2.hello("local ejb3.0 stateful");
        hello2.removeMethod();
        try {
            hello2.hello("this call should not go through");
            throw new Exception("bean should have been removed " + "after removeMethod()");
        } catch (NoSuchEJBException e) {
            System.out.println("Successfully caught EJBException after " + " accessing removed SFSB");
        }
        connection = qcFactory.createQueueConnection();
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueSender sender = session.createSender(clientQueue);
        TextMessage tmessage = session.createTextMessage();
        tmessage.setText("mdb() invoked");
        System.out.println("Sending message");
        sender.send(tmessage);
        System.out.println("message sent");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : NoSuchEJBException(javax.ejb.NoSuchEJBException) QueueConnection(javax.jms.QueueConnection) QueueSender(javax.jms.QueueSender) NoSuchEJBException(javax.ejb.NoSuchEJBException) EJBException(javax.ejb.EJBException) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage)

Aggregations

QueueConnection (javax.jms.QueueConnection)77 QueueSession (javax.jms.QueueSession)53 Test (org.junit.Test)41 TextMessage (javax.jms.TextMessage)36 Queue (javax.jms.Queue)33 Message (javax.jms.Message)26 MessageProducer (javax.jms.MessageProducer)20 QueueConnectionFactory (javax.jms.QueueConnectionFactory)20 Session (javax.jms.Session)17 JMSException (javax.jms.JMSException)16 InitialContext (javax.naming.InitialContext)15 QueueSender (javax.jms.QueueSender)14 XAQueueConnection (javax.jms.XAQueueConnection)14 ActiveMQRAConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory)14 ActiveMQRAConnectionFactoryImpl (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl)14 ActiveMQRAManagedConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory)14 QueueReceiver (javax.jms.QueueReceiver)13 MessageConsumer (javax.jms.MessageConsumer)12 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)9 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)6