Search in sources :

Example 46 with Connection

use of javax.jms.Connection 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 47 with Connection

use of javax.jms.Connection 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 48 with Connection

use of javax.jms.Connection 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 49 with Connection

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

the class EnvEntryTestCase method testEnvEntriesMDB.

@Test
public void testEnvEntriesMDB() throws Exception {
    ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
    Connection con = factory.createConnection();
    try {
        Destination dest = (Destination) ctx.lookup("java:jboss/queue/testEnvEntry");
        Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(dest);
        Queue replyQueue = session.createTemporaryQueue();
        MessageConsumer consumer = session.createConsumer(replyQueue);
        con.start();
        TextMessage msg = session.createTextMessage();
        msg.setJMSReplyTo(replyQueue);
        msg.setText("This is message one");
        producer.send(msg);
        MapMessage replyMsg = (MapMessage) consumer.receive(5000);
        Assert.assertNotNull(replyMsg);
        Assert.assertEquals(16, replyMsg.getInt("maxExceptions"));
        Assert.assertEquals(12, replyMsg.getInt("numExceptions"));
        Assert.assertEquals(7, replyMsg.getInt("minExceptions"));
    } finally {
        con.close();
    }
}
Also used : Destination(javax.jms.Destination) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) MapMessage(javax.jms.MapMessage) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 50 with Connection

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

the class TestEnvEntryMDBean method onMessage.

public void onMessage(Message msg) {
    log.trace("OnMessage working...");
    try {
        Destination destination = msg.getJMSReplyTo();
        Connection conn = connectionFactory.createConnection();
        try {
            Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer replyProducer = session.createProducer(destination);
            MapMessage replyMsg = session.createMapMessage();
            replyMsg.setJMSCorrelationID(msg.getJMSMessageID());
            replyMsg.setInt("maxExceptions", maxExceptions);
            replyMsg.setInt("numExceptions", numExceptions);
            replyMsg.setInt("minExceptions", minExceptions);
            // System.err.println("reply to: " + destination);
            // System.err.println("maxExceptions: " + maxExceptions);
            // System.err.println("numExceptions: " + numExceptions);
            // System.err.println("minExceptions: " + minExceptions);
            replyProducer.send(replyMsg);
        } finally {
            conn.close();
        }
    } catch (JMSException e) {
        throw new RuntimeException(e);
    }
}
Also used : Destination(javax.jms.Destination) MapMessage(javax.jms.MapMessage) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Aggregations

Connection (javax.jms.Connection)124 Session (javax.jms.Session)88 Test (org.junit.Test)62 MessageProducer (javax.jms.MessageProducer)46 ConnectionFactory (javax.jms.ConnectionFactory)44 Message (javax.jms.Message)38 MessageConsumer (javax.jms.MessageConsumer)38 JMSException (javax.jms.JMSException)32 TextMessage (javax.jms.TextMessage)32 QueueConnection (javax.jms.QueueConnection)22 TopicConnection (javax.jms.TopicConnection)22 Destination (javax.jms.Destination)15 Queue (javax.jms.Queue)12 QueueConnectionFactory (javax.jms.QueueConnectionFactory)11 TopicConnectionFactory (javax.jms.TopicConnectionFactory)11 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 TransactionStatus (org.springframework.transaction.TransactionStatus)7 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)7 TransactionAttribute (javax.ejb.TransactionAttribute)4