Search in sources :

Example 61 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class JMSFailoverListenerTest method testAutomaticFailover.

// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testAutomaticFailover() throws Exception {
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc);
    jbcf.setReconnectAttempts(-1);
    jbcf.setBlockOnDurableSend(true);
    jbcf.setBlockOnNonDurableSend(true);
    // Note we set consumer window size to a value so we can verify that consumer credit re-sending
    // works properly on failover
    // The value is small enough that credits will have to be resent several time
    final int numMessages = 10;
    final int bodySize = 1000;
    jbcf.setConsumerWindowSize(numMessages * bodySize / 10);
    ActiveMQConnection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
    MyFailoverListener listener = new MyFailoverListener();
    conn.setFailoverListener(listener);
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    SimpleString jmsQueueName = new SimpleString("myqueue");
    coreSession.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
    Queue queue = sess.createQueue("myqueue");
    MessageProducer producer = sess.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    MessageConsumer consumer = sess.createConsumer(queue);
    byte[] body = RandomUtil.randomBytes(bodySize);
    for (int i = 0; i < numMessages; i++) {
        BytesMessage bm = sess.createBytesMessage();
        bm.writeBytes(body);
        producer.send(bm);
    }
    conn.start();
    JMSFailoverListenerTest.log.info("sent messages and started connection");
    Thread.sleep(2000);
    JMSUtil.crash(liveServer, ((ActiveMQSession) sess).getCoreSession());
    Assert.assertEquals(FailoverEventType.FAILURE_DETECTED, listener.get(0));
    for (int i = 0; i < numMessages; i++) {
        JMSFailoverListenerTest.log.info("got message " + i);
        BytesMessage bm = (BytesMessage) consumer.receive(1000);
        Assert.assertNotNull(bm);
        Assert.assertEquals(body.length, bm.getBodyLength());
    }
    TextMessage tm = (TextMessage) consumer.receiveNoWait();
    Assert.assertNull(tm);
    Assert.assertEquals(FailoverEventType.FAILOVER_COMPLETED, listener.get(1));
    conn.close();
    Assert.assertEquals("Expected 2 FailoverEvents to be triggered", 2, listener.size());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BytesMessage(javax.jms.BytesMessage) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 62 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class JMSFailoverListenerTest method testManualFailover.

@Test
public void testManualFailover() throws Exception {
    ActiveMQConnectionFactory jbcfLive = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    jbcfLive.setBlockOnNonDurableSend(true);
    jbcfLive.setBlockOnDurableSend(true);
    ActiveMQConnectionFactory jbcfBackup = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY, backupParams));
    jbcfBackup.setBlockOnNonDurableSend(true);
    jbcfBackup.setBlockOnDurableSend(true);
    jbcfBackup.setInitialConnectAttempts(-1);
    jbcfBackup.setReconnectAttempts(-1);
    ActiveMQConnection connLive = (ActiveMQConnection) jbcfLive.createConnection();
    MyFailoverListener listener = new MyFailoverListener();
    connLive.setFailoverListener(listener);
    Session sessLive = connLive.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSessionLive = ((ActiveMQSession) sessLive).getCoreSession();
    SimpleString jmsQueueName = new SimpleString("myqueue");
    coreSessionLive.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
    Queue queue = sessLive.createQueue("myqueue");
    final int numMessages = 1000;
    MessageProducer producerLive = sessLive.createProducer(queue);
    for (int i = 0; i < numMessages; i++) {
        TextMessage tm = sessLive.createTextMessage("message" + i);
        producerLive.send(tm);
    }
    // Note we block on P send to make sure all messages get to server before failover
    JMSUtil.crash(liveServer, coreSessionLive);
    Assert.assertEquals(FailoverEventType.FAILURE_DETECTED, listener.get(0));
    connLive.close();
    // Now recreate on backup
    Connection connBackup = jbcfBackup.createConnection();
    Session sessBackup = connBackup.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumerBackup = sessBackup.createConsumer(queue);
    connBackup.start();
    for (int i = 0; i < numMessages; i++) {
        TextMessage tm = (TextMessage) consumerBackup.receive(1000);
        Assert.assertNotNull(tm);
        Assert.assertEquals("message" + i, tm.getText());
    }
    TextMessage tm = (TextMessage) consumerBackup.receiveNoWait();
    Assert.assertEquals(FailoverEventType.FAILOVER_FAILED, listener.get(1));
    Assert.assertEquals("Expected 2 FailoverEvents to be triggered", 2, listener.size());
    Assert.assertNull(tm);
    connBackup.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 63 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class JMSReconnectTest method testReconnectSameNodeServerRestartedWithNonDurableSubOrTempQueue.

// Test that non durable JMS sub gets recreated in auto reconnect
private void testReconnectSameNodeServerRestartedWithNonDurableSubOrTempQueue(final boolean nonDurableSub) throws Exception {
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    jbcf.setReconnectAttempts(-1);
    Connection conn = jbcf.createConnection();
    MyExceptionListener listener = new MyExceptionListener();
    conn.setExceptionListener(listener);
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    Destination dest;
    if (nonDurableSub) {
        coreSession.createQueue("mytopic", "blahblah", null, false);
        dest = ActiveMQJMSClient.createTopic("mytopic");
    } else {
        dest = sess.createTemporaryQueue();
    }
    MessageProducer producer = sess.createProducer(dest);
    // Create a non durable subscriber
    MessageConsumer consumer = sess.createConsumer(dest);
    this.server.stop();
    this.server.start();
    // Allow client some time to reconnect
    Thread.sleep(3000);
    final int numMessages = 100;
    byte[] body = RandomUtil.randomBytes(1000);
    for (int i = 0; i < numMessages; i++) {
        BytesMessage bm = sess.createBytesMessage();
        bm.writeBytes(body);
        producer.send(bm);
    }
    conn.start();
    for (int i = 0; i < numMessages; i++) {
        BytesMessage bm = (BytesMessage) consumer.receive(1000);
        Assert.assertNotNull(bm);
        Assert.assertEquals(body.length, bm.getBodyLength());
    }
    TextMessage tm = (TextMessage) consumer.receiveNoWait();
    Assert.assertNull(tm);
    conn.close();
    Assert.assertNotNull(listener.e);
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) BytesMessage(javax.jms.BytesMessage) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession)

Example 64 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class ReattachExample method stopStartAcceptor.

// To do this we send a management message to close the acceptor, we do this on a different
// connection factory which uses a different remoting connection so we can still send messages
// when the main connection has been stopped
private static void stopStartAcceptor(final boolean stop) throws Exception {
    ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61617");
    Connection connection = null;
    try {
        connection = cf.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
        MessageProducer producer = session.createProducer(managementQueue);
        connection.start();
        Message m = session.createMessage();
        String oper = stop ? "stop" : "start";
        JMSManagementHelper.putOperationInvocation(m, "core.acceptor.netty-acceptor", oper);
        producer.send(m);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) Session(javax.jms.Session)

Example 65 with ActiveMQConnectionFactory

use of org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class PreacknowledgeExample method main.

public static void main(final String[] args) throws Exception {
    Connection connection = null;
    try {
        // Step 2. instantiate the queue object
        Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
        // new connection factory
        ConnectionFactory cf = new ActiveMQConnectionFactory();
        // Step 3. Create a the JMS objects
        connection = cf.createConnection();
        Session session = connection.createSession(false, ActiveMQJMSConstants.PRE_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(queue);
        MessageConsumer messageConsumer = session.createConsumer(queue);
        // Step 4. Create and send a message
        TextMessage message1 = session.createTextMessage("This is a text message 1");
        producer.send(message1);
        System.out.println("Sent message: " + message1.getText());
        // Step 5. Print out the message count of the queue. The queue contains one message as expected
        // delivery has not yet started on the queue
        int count = getMessageCount(connection);
        System.out.println("Queue message count is " + count);
        // Step 6. Start the Connection, delivery will now start. Give a little time for delivery to occur.
        connection.start();
        Thread.sleep(1000);
        // Step 7. Print out the message countof the queue. It should now be zero, since the message has
        // already been acknowledged even before the consumer has received it.
        count = getMessageCount(connection);
        System.out.println("Queue message count is now " + count);
        if (count != 0) {
            throw new IllegalStateException("Queue message count is not 0.");
        }
        // Step 8. Finally, receive the message
        TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
        System.out.println("Received message: " + messageReceived.getText());
    } finally {
        // Step 9. Be sure to close our resources!
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) QueueConnection(javax.jms.QueueConnection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) QueueSession(javax.jms.QueueSession) Session(javax.jms.Session)

Aggregations

ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)221 Test (org.junit.Test)141 Connection (javax.jms.Connection)84 Session (javax.jms.Session)84 MessageProducer (javax.jms.MessageProducer)63 MessageConsumer (javax.jms.MessageConsumer)60 TextMessage (javax.jms.TextMessage)49 Queue (javax.jms.Queue)48 ConnectionFactory (javax.jms.ConnectionFactory)35 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)27 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)26 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)24 ActiveMQResourceAdapter (org.apache.activemq.artemis.ra.ActiveMQResourceAdapter)24 URI (java.net.URI)22 JMSException (javax.jms.JMSException)20 Message (javax.jms.Message)19 DiscoveryGroupConfiguration (org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration)19 InitialContext (javax.naming.InitialContext)16 Context (javax.naming.Context)15 Hashtable (java.util.Hashtable)14