home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

ActiveMQ example source code file (QueueDuplicatesTest.java)

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

activemqconnectionfactory, date, exception, exception, message, message, queueduplicatestest, sendingthread, session, session, simpleconsumer, string, string, text, thread, util

The ActiveMQ QueueDuplicatesTest.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.usecases;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import junit.framework.TestCase;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QueueDuplicatesTest extends TestCase {

    private static final Logger LOG = LoggerFactory.getLogger(QueueDuplicatesTest.class);

    private static DateFormat formatter = new SimpleDateFormat("HH:mm:ss SSS");
    private String brokerUrl;
    private String subject;
    private Connection brokerConnection;

    public QueueDuplicatesTest(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        String peerUrl = "peer://localhost:6099";

        subject = this.getClass().getName();

        ActiveMQConnectionFactory fac = createFactory(peerUrl);
        brokerConnection = fac.createConnection();
        brokerConnection.start();
    }

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

    public void testDuplicates() {
        try {
            // Get Session
            Session session = createSession(brokerConnection);
            // create consumer
            Destination dest = session.createQueue(subject);
            MessageConsumer consumer = session.createConsumer(dest);
            // subscribe to queue
            consumer.setMessageListener(new SimpleConsumer());
            // create producer
            Thread sendingThread = new SendingThread(brokerUrl, subject);
            // start producer
            sendingThread.start();
            // wait about 5 seconds
            Thread.sleep(5000);
            // unsubscribe consumer
            consumer.close();
            // wait another 5 seconds
            Thread.sleep(5000);
            // create new consumer
            consumer = session.createConsumer(dest);
            // subscribe to queue
            consumer.setMessageListener(new SimpleConsumer());
            // sleep a little while longer
            Thread.sleep(15000);
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Session createSession(Connection peerConnection) throws JMSException {
        // Connect using peer to peer connection
        Session session = peerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        return session;
    }

    private ActiveMQConnectionFactory createFactory(String brokerUrl) {
        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
        cf.setBrokerURL(brokerUrl);

        return cf;
    }

    private class SendingThread extends Thread {
        private String subject;

        SendingThread(String brokerUrl, String subject) {
            this.subject = subject;
            setDaemon(false);
        }

        public void run() {
            try {
                Session session = createSession(brokerConnection);
                Destination dest = session.createQueue(subject);
                MessageProducer producer = session.createProducer(dest);
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                for (int i = 0; i < 20; i++) {
                    String txt = "Text Message: " + i;
                    TextMessage msg = session.createTextMessage(txt);
                    producer.send(msg);
                    LOG.info(formatter.format(new Date()) + " Sent ==> " + msg + " to " + subject);
                    Thread.sleep(1000);
                }
                session.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static class SimpleConsumer implements MessageListener {
        private Map<String, Message> msgs = new HashMap();

        public void onMessage(Message message) {
            LOG.info(formatter.format(new Date()) + " SimpleConsumer Message Received: " + message);
            try {
                String id = message.getJMSMessageID();
                assertNull("Message is duplicate: " + id, msgs.get(id));
                msgs.put(id, message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Other ActiveMQ examples (source code examples)

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

new blog posts

 

Copyright 1998-2016 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.