Search in sources :

Example 51 with QueueConnection

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

the class OutgoingConnectionNoJTATest method testSimpleMessageSendAndReceiveSessionTransacted.

@Test
public void testSimpleMessageSendAndReceiveSessionTransacted() throws Exception {
    setupDLQ(10);
    resourceAdapter = newResourceAdapter();
    MyBootstrapContext ctx = new MyBootstrapContext();
    resourceAdapter.start(ctx);
    ActiveMQRAManagedConnectionFactory mcf = new ActiveMQRAManagedConnectionFactory();
    mcf.setAllowLocalTransactions(true);
    mcf.setResourceAdapter(resourceAdapter);
    ActiveMQRAConnectionFactory qraConnectionFactory = new ActiveMQRAConnectionFactoryImpl(mcf, qraConnectionManager);
    QueueConnection queueConnection = qraConnectionFactory.createQueueConnection();
    Session s = queueConnection.createSession(true, Session.SESSION_TRANSACTED);
    Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
    MessageProducer mp = s.createProducer(q);
    MessageConsumer consumer = s.createConsumer(q);
    Message message = s.createTextMessage("test");
    mp.send(message);
    s.commit();
    queueConnection.start();
    TextMessage textMessage = (TextMessage) consumer.receive(1000);
    assertNotNull(textMessage);
    assertEquals(textMessage.getText(), "test");
    s.rollback();
    textMessage = (TextMessage) consumer.receive(1000);
    assertNotNull(textMessage);
    assertEquals(textMessage.getText(), "test");
    s.commit();
    textMessage = (TextMessage) consumer.receiveNoWait();
    assertNull(textMessage);
}
Also used : ActiveMQRAManagedConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) QueueConnection(javax.jms.QueueConnection) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ActiveMQRAConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) ActiveMQRAConnectionFactoryImpl(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl) Session(javax.jms.Session) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 52 with QueueConnection

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

the class CreateTemporaryQueueBeforeStartTest method testTemporaryQueueConsumer.

public void testTemporaryQueueConsumer() throws Exception {
    final int number = 20;
    final AtomicInteger count = new AtomicInteger(0);
    for (int i = 0; i < number; i++) {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    QueueConnection connection = createConnection();
                    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
                    Queue queue = session.createTemporaryQueue();
                    session.createReceiver(queue);
                    connection.start();
                    if (count.incrementAndGet() >= number) {
                        synchronized (count) {
                            count.notify();
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        thread.start();
    }
    int maxWaitTime = 20000;
    synchronized (count) {
        long waitTime = maxWaitTime;
        long start = System.currentTimeMillis();
        while (count.get() < number) {
            if (waitTime <= 0) {
                break;
            } else {
                count.wait(waitTime);
                waitTime = maxWaitTime - (System.currentTimeMillis() - start);
            }
        }
    }
    assertTrue("Unexpected count: " + count, count.get() == number);
}
Also used : QueueConnection(javax.jms.QueueConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Example 53 with QueueConnection

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

the class ConnectionFactoryTest method testQueueConnectionFactory.

/**
 * Test that ConnectionFactory can be cast to QueueConnectionFactory and QueueConnection can be
 * created.
 */
@Test
public void testQueueConnectionFactory() throws Exception {
    deployConnectionFactory(0, JMSFactoryType.QUEUE_CF, "CF_QUEUE_XA_FALSE", "/CF_QUEUE_XA_FALSE");
    QueueConnectionFactory qcf = (QueueConnectionFactory) ic.lookup("/CF_QUEUE_XA_FALSE");
    QueueConnection qc = qcf.createQueueConnection();
    qc.close();
    undeployConnectionFactory("CF_QUEUE_XA_FALSE");
}
Also used : XAQueueConnection(javax.jms.XAQueueConnection) QueueConnection(javax.jms.QueueConnection) XAQueueConnectionFactory(javax.jms.XAQueueConnectionFactory) QueueConnectionFactory(javax.jms.QueueConnectionFactory) Test(org.junit.Test)

Example 54 with QueueConnection

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

the class ClientKickoffExample 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. Perform a lookup on the Connection Factory
        QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 3.Create a JMS Connection
        connection = cf.createQueueConnection();
        // Step 4. Set an exception listener on the connection to be notified after a problem occurred
        final AtomicReference<JMSException> exception = new AtomicReference<>();
        connection.setExceptionListener(new ExceptionListener() {

            @Override
            public void onException(final JMSException e) {
                exception.set(e);
            }
        });
        // Step 5. We start the connection
        connection.start();
        // Step 6. Create an ActiveMQServerControlMBean proxy to manage the server
        ObjectName on = ObjectNameBuilder.DEFAULT.getActiveMQServerObjectName();
        JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(JMX_URL), new HashMap<String, String>());
        MBeanServerConnection mbsc = connector.getMBeanServerConnection();
        ActiveMQServerControl serverControl = MBeanServerInvocationHandler.newProxyInstance(mbsc, on, ActiveMQServerControl.class, false);
        // Step 7. List the remote address connected to the server
        System.out.println("List of remote addresses connected to the server:");
        System.out.println("----------------------------------");
        String[] remoteAddresses = serverControl.listRemoteAddresses();
        for (String remoteAddress : remoteAddresses) {
            System.out.println(remoteAddress);
        }
        System.out.println("----------------------------------");
        // Step 8. Close the connections for the 1st remote address and kickoff the client
        serverControl.closeConnectionsForAddress(remoteAddresses[0]);
        // Sleep a little bit so that the stack trace from the server won't be
        // mingled with the JMSException received on the ExceptionListener
        Thread.sleep(1000);
        // Step 9. Display the exception received by the connection's ExceptionListener
        System.err.println("\nException received from the server:");
        System.err.println("----------------------------------");
        exception.get().printStackTrace();
        System.err.println("----------------------------------");
    } finally {
        // Step 10. Be sure to close the resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) QueueConnectionFactory(javax.jms.QueueConnectionFactory) JMSException(javax.jms.JMSException) AtomicReference(java.util.concurrent.atomic.AtomicReference) InitialContext(javax.naming.InitialContext) ObjectName(javax.management.ObjectName) QueueConnection(javax.jms.QueueConnection) JMXConnector(javax.management.remote.JMXConnector) ExceptionListener(javax.jms.ExceptionListener) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 55 with QueueConnection

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

the class PreacknowledgeExample method getMessageCount.

// To do this we send a management message to get the message count.
// In real life you wouldn't create a new session every time you send a management message
private static int getMessageCount(final Connection connection) throws Exception {
    QueueSession session = ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
    QueueRequestor requestor = new QueueRequestor(session, managementQueue);
    connection.start();
    Message m = session.createMessage();
    JMSManagementHelper.putAttribute(m, ResourceNames.QUEUE + "exampleQueue", "messageCount");
    Message response = requestor.request(m);
    int messageCount = (Integer) JMSManagementHelper.getResult(response, Integer.class);
    return messageCount;
}
Also used : QueueRequestor(javax.jms.QueueRequestor) QueueConnection(javax.jms.QueueConnection) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Queue(javax.jms.Queue) 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