Search in sources :

Example 56 with QueueConnection

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

the class JMXOverSSLExample method main.

public static void main(final String[] args) throws Exception {
    QueueConnection connection = null;
    InitialContext initialContext = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Perfom a lookup on the queue
        Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
        // Step 3. Perform a lookup on the Connection Factory
        QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 4.Create a JMS Connection
        connection = cf.createQueueConnection();
        // Step 5. Create a JMS Session
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 6. Create a JMS Message Producer
        MessageProducer producer = session.createProducer(queue);
        // Step 7. Create a Text Message
        TextMessage message = session.createTextMessage("This is a text message");
        System.out.println("Sent message: " + message.getText());
        // Step 8. Send the Message
        producer.send(message);
        // Step 9. Retrieve the ObjectName of the queue. This is used to identify the server resources to manage
        ObjectName on = ObjectNameBuilder.DEFAULT.getQueueObjectName(SimpleString.toSimpleString(queue.getQueueName()), SimpleString.toSimpleString(queue.getQueueName()), RoutingType.ANYCAST);
        // Step 10. Create JMX Connector to connect to the server's MBeanServer
        // we dont actually need credentials as the guest login i sused but this is how its done
        HashMap env = new HashMap();
        String[] creds = { "guest", "guest" };
        env.put(JMXConnector.CREDENTIALS, creds);
        System.setProperty("javax.net.ssl.trustStore", args[0] + "client-side-truststore.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "secureexample");
        System.setProperty("javax.net.ssl.keyStore", args[0] + "client-side-keystore.jks");
        System.setProperty("javax.net.ssl.keyStorePassword", "secureexample");
        JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMXOverSSLExample.JMX_URL), env);
        // Step 11. Retrieve the MBeanServerConnection
        MBeanServerConnection mbsc = connector.getMBeanServerConnection();
        // Step 12. Create a QueueControl proxy to manage the queue on the server
        QueueControl queueControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, QueueControl.class, false);
        // Step 13. Display the number of messages in the queue
        System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages");
        // Step 14. Remove the message sent at step #8
        System.out.println("message has been removed: " + queueControl.removeMessages(null));
        // Step 15. Display the number of messages in the queue
        System.out.println(queueControl.getName() + " contains " + queueControl.getMessageCount() + " messages");
        // Step 16. We close the JMX connector
        connector.close();
        // Step 17. Create a JMS Message Consumer on the queue
        MessageConsumer messageConsumer = session.createConsumer(queue);
        // Step 18. Start the Connection
        connection.start();
        // Step 19. Trying to receive a message. Since the only message in the queue was removed by a management
        // operation, there is none to consume.
        // The call will timeout after 5000ms and messageReceived will be null
        TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
        if (messageReceived != null) {
            throw new IllegalStateException("message should be null!");
        }
        System.out.println("Received message: " + messageReceived);
    } finally {
        // Step 20. Be sure to close the resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (connection != null) {
            connection.close();
        }
        System.clearProperty("javax.net.ssl.trustStore");
        System.clearProperty("javax.net.ssl.trustStorePassword");
        System.clearProperty("javax.net.ssl.keyStore");
        System.clearProperty("javax.net.ssl.keyStorePassword");
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MessageConsumer(javax.jms.MessageConsumer) HashMap(java.util.HashMap) QueueConnectionFactory(javax.jms.QueueConnectionFactory) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) InitialContext(javax.naming.InitialContext) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) ObjectName(javax.management.ObjectName) QueueConnection(javax.jms.QueueConnection) JMXConnector(javax.management.remote.JMXConnector) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 57 with QueueConnection

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

the class ManagementExample method main.

public static void main(final String[] args) throws Exception {
    QueueConnection connection = null;
    InitialContext initialContext = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Perfom a lookup on the queue
        Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
        // Step 3. Perform a lookup on the Connection Factory
        QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 4.Create a JMS Connection
        connection = cf.createQueueConnection();
        // Step 5. Create a JMS Session
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 6. Create a JMS Message Producer
        MessageProducer producer = session.createProducer(queue);
        // Step 7. Create a Text Message
        TextMessage message = session.createTextMessage("This is a text message");
        System.out.println("Sent message: " + message.getText());
        // Step 8. Send the Message
        producer.send(message);
        // Step 9. create the JMS management queue.
        // It is a "special" queue and it is not looked up from JNDI but constructed directly
        Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
        // Step 10. Create a QueueRequestor for the management queue (see queue-requestor example)
        QueueRequestor requestor = new QueueRequestor(session, managementQueue);
        // Step 11. Start the Connection to allow the queue requestor to receive replies
        connection.start();
        // Step 12. Create a JMS message which is used to send a management message
        Message m = session.createMessage();
        // Step 13. Use a helper class to fill the JMS message with management information:
        // * the name of the resource to manage
        // * in this case, we want to retrieve the value of the messageCount of the queue
        JMSManagementHelper.putAttribute(m, ResourceNames.QUEUE + "exampleQueue", "messageCount");
        // Step 14. Use the requestor to send the request and wait for the reply
        Message reply = requestor.request(m);
        // Step 15. Use a helper class to retrieve the operation result
        int messageCount = (Integer) JMSManagementHelper.getResult(reply, Integer.class);
        System.out.println(queue.getQueueName() + " contains " + messageCount + " messages");
        // Step 16. Create another JMS message to use as a management message
        m = session.createMessage();
        // Step 17. Use a helper class to fill the JMS message with management information:
        // * the object name of the resource to manage (i.e. the queue)
        // * in this case, we want to call the "removeMessage" operation with the JMS MessageID
        // of the message sent to the queue in step 8.
        JMSManagementHelper.putOperationInvocation(m, ResourceNames.QUEUE + "exampleQueue", "removeMessages", FilterConstants.ACTIVEMQ_USERID + " = '" + message.getJMSMessageID() + "'");
        // Step 18 Use the requestor to send the request and wait for the reply
        reply = requestor.request(m);
        // Step 19. Use a helper class to check that the operation has succeeded
        boolean success = JMSManagementHelper.hasOperationSucceeded(reply);
        System.out.println("operation invocation has succeeded: " + success);
        // Step 20. Use a helper class to retrieve the operation result
        // in that case, a long which is 1 if the message was removed, 0 else
        boolean messageRemoved = 1 == (long) JMSManagementHelper.getResult(reply);
        System.out.println("message has been removed: " + messageRemoved);
        // Step 21. Create a JMS Message Consumer on the queue
        MessageConsumer messageConsumer = session.createConsumer(queue);
        // Step 22. Trying to receive a message. Since the only message in the queue was removed by a management
        // operation,
        // there is none to consume. The call will timeout after 5000ms and messageReceived will be null
        TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
        System.out.println("Received message: " + messageReceived);
    } finally {
        // Step 23. Be sure to close the resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) QueueConnectionFactory(javax.jms.QueueConnectionFactory) InitialContext(javax.naming.InitialContext) QueueRequestor(javax.jms.QueueRequestor) QueueConnection(javax.jms.QueueConnection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage)

Example 58 with QueueConnection

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

the class JMSSecurityTest method testCreateQueueConnection.

@Test
public void testCreateQueueConnection() throws Exception {
    ActiveMQJAASSecurityManager securityManager = (ActiveMQJAASSecurityManager) server.getSecurityManager();
    securityManager.getConfiguration().addUser("IDo", "Exist");
    try {
        QueueConnection queueC = ((QueueConnectionFactory) cf).createQueueConnection("IDont", "Exist");
        fail("supposed to throw exception");
        queueC.close();
    } catch (JMSSecurityException e) {
    // expected
    }
    JMSContext ctx = cf.createContext("IDo", "Exist");
    ctx.close();
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueConnectionFactory(javax.jms.QueueConnectionFactory) JMSSecurityException(javax.jms.JMSSecurityException) ActiveMQJAASSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager) JMSContext(javax.jms.JMSContext) Test(org.junit.Test)

Example 59 with QueueConnection

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

the class NewQueueRequestorTest method testQueueRequestor.

@Test
public void testQueueRequestor() throws Exception {
    QueueConnection conn1 = null, conn2 = null;
    try {
        Queue queue1 = createQueue(true, "myQueue");
        conn1 = (QueueConnection) cf.createConnection();
        QueueSession sess1 = conn1.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueRequestor requestor = new QueueRequestor(sess1, queue1);
        conn1.start();
        // And the responder
        conn2 = (QueueConnection) cf.createConnection();
        QueueSession sess2 = conn2.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        TestMessageListener listener = new TestMessageListener(sess2);
        QueueReceiver receiver = sess2.createReceiver(queue1);
        receiver.setMessageListener(listener);
        conn2.start();
        Message m1 = sess1.createMessage();
        log.trace("Sending request message");
        TextMessage m2 = (TextMessage) requestor.request(m1);
        assertNotNull(m2);
        assertEquals("This is the response", m2.getText());
        requestor.close();
    } finally {
        conn1.close();
        conn2.close();
    }
}
Also used : QueueRequestor(javax.jms.QueueRequestor) QueueConnection(javax.jms.QueueConnection) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) QueueReceiver(javax.jms.QueueReceiver) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 60 with QueueConnection

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

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