alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

ActiveMQ example source code file (JmsDurableTopicSendReceiveTest.java)

This example ActiveMQ source code file (JmsDurableTopicSendReceiveTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - ActiveMQ tags/keywords

connection, destination, destination, exception, exception, jmsdurabletopicsendreceivetest, logger, message, messageconsumer, messageproducer, session, session, textmessage, textmessage

The ActiveMQ JmsDurableTopicSendReceiveTest.java source code

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.test.JmsTopicSendReceiveTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 */
public class JmsDurableTopicSendReceiveTest extends JmsTopicSendReceiveTest {
    private static final Logger LOG = LoggerFactory.getLogger(JmsDurableTopicSendReceiveTest.class);

    protected Connection connection2;
    protected Session session2;
    protected Session consumeSession2;
    protected MessageConsumer consumer2;
    protected MessageProducer producer2;
    protected Destination consumerDestination2;
    protected Destination producerDestination2;

    /**
     * Set up a durable suscriber test.
     * 
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception {
        this.durable = true;
        super.setUp();
    }

    /**
     * Test if all the messages sent are being received.
     * 
     * @throws Exception
     */
    public void testSendWhileClosed() throws Exception {
        connection2 = createConnection();
        connection2.setClientID("test");
        connection2.start();
        session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer2 = session2.createProducer(null);
        producer2.setDeliveryMode(deliveryMode);
        producerDestination2 = session2.createTopic(getProducerSubject() + "2");
        Thread.sleep(1000);

        consumeSession2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        consumerDestination2 = session2.createTopic(getConsumerSubject() + "2");
        consumer2 = consumeSession2.createDurableSubscriber((Topic)consumerDestination2, getName());
        Thread.sleep(1000);
        consumer2.close();
        TextMessage message = session2.createTextMessage("test");
        message.setStringProperty("test", "test");
        message.setJMSType("test");
        producer2.send(producerDestination2, message);
        LOG.info("Creating durable consumer");
        consumer2 = consumeSession2.createDurableSubscriber((Topic)consumerDestination2, getName());
        Message msg = consumer2.receive(1000);
        assertNotNull(msg);
        assertEquals(((TextMessage)msg).getText(), "test");
        assertEquals(msg.getJMSType(), "test");
        assertEquals(msg.getStringProperty("test"), "test");
        connection2.stop();
        connection2.close();
    }
}

Other ActiveMQ examples (source code examples)

Here is a short list of links related to this ActiveMQ JmsDurableTopicSendReceiveTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.