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

ActiveMQ example source code file (DestinationListenerTest.java)

This example ActiveMQ source code file (DestinationListenerTest.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

activemqconnection, activemqdestination, activemqqueue, activemqqueue, activemqtopic, brokerservice, brokerservice, exception, exception, new, session, set, the, the, util

The ActiveMQ DestinationListenerTest.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.advisory;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.jms.Session;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.EmbeddedBrokerTestSupport;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.command.ActiveMQDestination;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

/**
 * 
 */
public class DestinationListenerTest extends EmbeddedBrokerTestSupport implements DestinationListener {
    private static final transient Logger LOG = LoggerFactory.getLogger(DestinationListenerTest.class);
    protected ActiveMQConnection connection;
    protected ActiveMQQueue sampleQueue = new ActiveMQQueue("foo.bar");
    protected ActiveMQTopic sampleTopic = new ActiveMQTopic("cheese");
    protected List<ActiveMQDestination> newDestinations = new ArrayList();

    public void testDestiationSourceHasInitialDestinations() throws Exception {
        Thread.sleep(1000);

        DestinationSource destinationSource = connection.getDestinationSource();
        Set<ActiveMQQueue> queues = destinationSource.getQueues();
        Set<ActiveMQTopic> topics = destinationSource.getTopics();

        LOG.info("Queues: " + queues);
        LOG.info("Topics: " + topics);

        assertTrue("The queues should not be empty!", !queues.isEmpty());
        assertTrue("The topics should not be empty!", !topics.isEmpty());

        assertTrue("queues contains initial queue: " + queues, queues.contains(sampleQueue));
        assertTrue("topics contains initial topic: " + queues, topics.contains(sampleTopic));
    }

    public void testConsumerForcesNotificationOfNewDestination() throws Exception {
        // now lets cause a destination to be created
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        ActiveMQQueue newQueue = new ActiveMQQueue("Test.Cheese");
        session.createConsumer(newQueue);

        Thread.sleep(3000);

        assertThat(newQueue, isIn(newDestinations));

        LOG.info("New destinations are: " + newDestinations);
    }

    public void testProducerForcesNotificationOfNewDestination() throws Exception {
        // now lets cause a destination to be created
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        ActiveMQQueue newQueue = new ActiveMQQueue("Test.Beer");
        MessageProducer producer = session.createProducer(newQueue);
        TextMessage message = session.createTextMessage("<hello>world");
        producer.send(message);

        Thread.sleep(3000);

        assertThat(newQueue, isIn(newDestinations));

        LOG.info("New destinations are: " + newDestinations);
    }

    public void onDestinationEvent(DestinationEvent event) {
        ActiveMQDestination destination = event.getDestination();
        if (event.isAddOperation()) {
            LOG.info("Added:   " + destination);
            newDestinations.add(destination);
        }
        else {
            LOG.info("Removed: " + destination);
            newDestinations.remove(destination);
        }
    }

    protected void setUp() throws Exception {
        super.setUp();

        connection = (ActiveMQConnection) createConnection();
        connection.start();
        connection.getDestinationSource().setDestinationListener(this);
    }

    @Override
    protected BrokerService createBroker() throws Exception {
        BrokerService broker = super.createBroker();
        broker.setDestinations(new ActiveMQDestination[]{
                sampleQueue,
                sampleTopic
        });
        return broker;
    }

    protected void tearDown() throws Exception {
        if (connection != null) {
            connection.close();
        }
        super.tearDown();
    }
}

Other ActiveMQ examples (source code examples)

Here is a short list of links related to this ActiveMQ DestinationListenerTest.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.