• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 85
  • Last Modified:

activemq queue issue

I am trying below example


When i try to run the MEssageSender class how it ran even though there is existing Queue on ActiveMQ called JCG_QUEUE

package com.activemq.sender;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class MessageSender {
	//URL of the JMS server. DEFAULT_BROKER_URL will just mean that JMS server is on localhost
	private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
	// default broker URL is : tcp://localhost:61616"
	private static String subject = "JCG_QUEUE"; // Queue Name.You can create any/many queue names as per your requirement.	
	public static void main(String[] args) throws JMSException {		
		// Getting JMS connection from the server and starting it
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
		Connection connection = connectionFactory.createConnection();
		//Creating a non transactional session to send/receive JMS message.
		Session session = connection.createSession(false,
		//Destination represents here our queue 'JCG_QUEUE' on the JMS server. 
		//The queue will be created automatically on the server.
		Destination destination = session.createQueue(subject);	
		// MessageProducer is used for sending messages to the queue.
		MessageProducer producer = session.createProducer(destination);
		// We will send a small text message saying 'Hello World!!!' 
		TextMessage message = session
				.createTextMessage("Hello !!! Welcome to the world of ActiveMQ.");
		// Here we are sending our message!
		System.out.println("JCG printing@@ '" + message.getText() + "'");

Open in new window

in my code it was never said if queue already there delete and recreate etc?

please advise
1 Solution
mccarlIT Business Systems Analyst / Software DeveloperCommented:
A JMS client never creates a Queue directly. The createQueue method is a bit misleading, it doesn't create the queue on the server, it just creates the "Queue" object for use by your client code.

Typically, the JMS MQ administrator creates all the queues that are required, as an administrative task. However, ActiveMQ has a specific way that it can be configured (and it is this way by default) where it will automatically create a queue when the first messge is sent to it, or when the first receiver listening to a queue starts up. If the queue is already existing, it will just use that queue straight away.

in my code it was never said if queue already there delete and recreate etc?

Note, it DIDN'T delete and recreate, it just used the existing queue.

Hopefully, this all helps to explain what you are seeing. Let us know if you still have questions.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now