Search in sources :

Example 51 with TextMessage

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

the class HelloBean method sendMessage.

public String sendMessage() throws Exception {
    String destinationName = "java:jboss/exported/queue/TestQueue";
    Context ic = null;
    ConnectionFactory cf = null;
    Connection connection = null;
    try {
        ic = getInitialContext();
        cf = (ConnectionFactory) ic.lookup("java:/ConnectionFactory");
        Queue queue = (Queue) ic.lookup(destinationName);
        connection = cf.createConnection("guest", "guest");
        // we need to start connection for consumer
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = session.createProducer(queue);
        TextMessage message = session.createTextMessage("hello goodbye");
        TemporaryQueue replyQueue = session.createTemporaryQueue();
        message.setJMSReplyTo(replyQueue);
        sender.send(message);
        MessageConsumer consumer = session.createConsumer(replyQueue);
        TextMessage replyMsg = (TextMessage) consumer.receive(5000);
        log.trace("Message received:" + replyMsg);
        return replyMsg.getText();
    } finally {
        if (ic != null) {
            try {
                ic.close();
            } catch (Exception ignore) {
            }
        }
        closeConnection(connection);
    }
}
Also used : InitialContext(javax.naming.InitialContext) SessionContext(javax.ejb.SessionContext) Context(javax.naming.Context) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 52 with TextMessage

use of javax.jms.TextMessage 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 53 with TextMessage

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

the class RunAsTestCaseEJBMDB method testSendMessage.

@Test
public void testSendMessage() throws Exception {
    ConnectionFactory cf = null;
    Connection connection = null;
    Session session = null;
    try {
        cf = (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");
        Queue queue = (Queue) initialContext.lookup(QUEUE_NAME);
        connection = cf.createConnection("guest", "guest");
        //for consumer we need to start connection
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = session.createProducer(queue);
        TemporaryQueue replyQueue = session.createTemporaryQueue();
        TextMessage message = session.createTextMessage("hello goodbye");
        message.setJMSReplyTo(replyQueue);
        sender.send(message);
        log.trace("testSendMessage(): Message sent!");
        MessageConsumer consumer = session.createConsumer(replyQueue);
        Message replyMsg = consumer.receive(5000);
        Assert.assertNotNull(replyMsg);
        Assert.assertTrue(replyMsg instanceof TextMessage);
        String actual = ((TextMessage) replyMsg).getText();
        Assert.assertEquals("Howdy Fred! GoodBye user1", actual);
    } finally {
        if (session != null) {
            session.close();
        }
        closeConnection(connection);
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 54 with TextMessage

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

the class RunAsMDBUnitTestCase method testSendMessage.

@Test
public void testSendMessage() throws Exception {
    ConnectionFactory connFactory = lookup("ConnectionFactory", ConnectionFactory.class);
    Connection conn = connFactory.createConnection();
    conn.start();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue replyQueue = session.createTemporaryQueue();
    TextMessage msg = session.createTextMessage("Hello world");
    msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
    msg.setJMSReplyTo(replyQueue);
    Queue queue = lookup("java:jboss/" + queueName, Queue.class);
    MessageProducer producer = session.createProducer(queue);
    producer.send(msg);
    MessageConsumer consumer = session.createConsumer(replyQueue);
    Message replyMsg = consumer.receive(5000);
    Assert.assertNotNull(replyMsg);
    if (replyMsg instanceof ObjectMessage) {
        Exception e = (Exception) ((ObjectMessage) replyMsg).getObject();
        throw e;
    }
    Assert.assertTrue(replyMsg instanceof TextMessage);
    String actual = ((TextMessage) replyMsg).getText();
    Assert.assertEquals("SUCCESS", actual);
    consumer.close();
    producer.close();
    session.close();
    conn.close();
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ObjectMessage(javax.jms.ObjectMessage) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 55 with TextMessage

use of javax.jms.TextMessage 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");
    SecurityClient client = this.login();
    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();
        }
        client.logout();
    }
}
Also used : SecurityClient(org.jboss.security.client.SecurityClient) 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) ObjectMessage(javax.jms.ObjectMessage) 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)

Aggregations

TextMessage (javax.jms.TextMessage)231 Test (org.junit.Test)92 Session (javax.jms.Session)75 MessageProducer (javax.jms.MessageProducer)71 Message (javax.jms.Message)70 JMSException (javax.jms.JMSException)64 Connection (javax.jms.Connection)44 Destination (javax.jms.Destination)44 MessageConsumer (javax.jms.MessageConsumer)44 ObjectMessage (javax.jms.ObjectMessage)25 BytesMessage (javax.jms.BytesMessage)22 Queue (javax.jms.Queue)21 QueueSession (javax.jms.QueueSession)20 StubTextMessage (org.springframework.jms.StubTextMessage)18 ConnectionFactory (javax.jms.ConnectionFactory)13 QueueConnection (javax.jms.QueueConnection)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 MapMessage (javax.jms.MapMessage)11 Topic (javax.jms.Topic)11 JMSContext (javax.jms.JMSContext)9