You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by jainmanglesh <ja...@rediffmail.com> on 2013/01/02 11:02:56 UTC

Re: How to setup multiple queues

Hi
Thank you very much for your reply.

my requirement is like: 
I want to create a list of activemq (which I am trying to create using for
loop as in code). These queues will be used by third party to send message
and on the other end used by third party to consume the messages on queue.

Third party sending messages ----> q1, q2,q3... q10  ---> Third party
consuming messages.

A random queue will be selected by both third party to put and take the
messages from queue.

I cannot have a code to produce the message and put into queue (sorry about
adding that in the code above).
However as you said, If I dont have producer it does not create the queue
(as I cannot see on admin console).

The only thing is I want to create Queues, that's all. and third party will
be using them to put and take messages from its ends.

Thank you.



--
View this message in context: http://activemq.2283324.n4.nabble.com/How-to-setup-multiple-queues-tp3092113p4661197.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: How to setup multiple queues

Posted by jainmanglesh <ja...@rediffmail.com>.
Thank you very much,
I have created queue and created a junk destination to create the queue
physically (I would discard the junk message at consumer side), this way it
will create queue on the server.
Then my third party producer and consumer can refer the queue created on the
server.

Now the only problem i need to sort out is: admin console (or under lying
activemq) shows pending messages but doesnt consumer those messages. Once I
restart the activemq server, it consumes the messages.

Thanks.



--
View this message in context: http://activemq.2283324.n4.nabble.com/How-to-setup-multiple-queues-tp3092113p4661212.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: How to setup multiple queues

Posted by Christian Posta <ch...@gmail.com>.
Yes, I think we understand what you're trying to do, but it's not clear why
you need step 1. The queues don't need to be created ahead of time (like,
for example in IBM MQ series). They will be created automatically when a
producer tries to send to it or a consumer tries to receive from it.

JMS doesn't provide facilities to "create queues" on the broker as that's
an admin feature (see comment for Session#createQueue: "Note that this
method is not for creating the physical queue. The physical creation of
queues is an administrative task and is not to be initiated by the JMS
API.")

The broker does provide runtime abilities to create queues in an
administrative role. You could connect via JMX and the BrokerView has a
"addQueue" method you can call programmatically.


On Wed, Jan 2, 2013 at 6:22 AM, jainmanglesh <ja...@rediffmail.com>wrote:

> I think I am not clear in providing information here. Let me try one more
> time.
>
> So according to requirement, I need to create queue at run time in java
> code
> (because I would come to know the number of queues to create at run time).
> this would be independent class.
> There are three part (you can say three classes) of the whole
> project(requirement here)
> 1. a class creating queue
> 2. a producer class putting messages/events on queues created in step1.
> 3. a consumer class taking messages/events from queues created in step1.
>
> The queues created in step1 will be used by producer/consumer in step2/3.
> Both producer and consumer are like third party using queues created in
> step1.
> Unfortunately I cannot have producer using queues created in step1.
>
> I am not sure if I am still clear or not. i tried to find this kinda
> requirement on internet but I could not get much help except a method to
> create queue at runtime (createQueue) or having queues at front (in xml
> file)
>
> for the comment "using a producer to send to x, will cause x (queue or
> topic) to be created"
> the producer is in separate class.
>
> The only other option is to create physical queue by just creating a junk
> producer to send junk message/even at first, which will atleast create
> queues on server which could be later used by the third party producer. and
> the same way, at the consumer side I can write logic to throw away junk
> message/event.
>
> Did I make myself clear here :-( .
>
>
>
>
>
> --
> View this message in context:
> http://activemq.2283324.n4.nabble.com/How-to-setup-multiple-queues-tp3092113p4661203.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>



-- 
*Christian Posta*
http://www.christianposta.com/blog
twitter: @christianposta

Re: How to setup multiple queues

Posted by jainmanglesh <ja...@rediffmail.com>.
I think I am not clear in providing information here. Let me try one more
time.

So according to requirement, I need to create queue at run time in java code
(because I would come to know the number of queues to create at run time).
this would be independent class.
There are three part (you can say three classes) of the whole
project(requirement here)
1. a class creating queue
2. a producer class putting messages/events on queues created in step1.
3. a consumer class taking messages/events from queues created in step1.

The queues created in step1 will be used by producer/consumer in step2/3.
Both producer and consumer are like third party using queues created in
step1.
Unfortunately I cannot have producer using queues created in step1. 

I am not sure if I am still clear or not. i tried to find this kinda
requirement on internet but I could not get much help except a method to
create queue at runtime (createQueue) or having queues at front (in xml
file)

for the comment "using a producer to send to x, will cause x (queue or
topic) to be created"
the producer is in separate class.

The only other option is to create physical queue by just creating a junk
producer to send junk message/even at first, which will atleast create
queues on server which could be later used by the third party producer. and
the same way, at the consumer side I can write logic to throw away junk
message/event.

Did I make myself clear here :-( .





--
View this message in context: http://activemq.2283324.n4.nabble.com/How-to-setup-multiple-queues-tp3092113p4661203.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: How to setup multiple queues

Posted by Gary Tully <ga...@gmail.com>.
note: activemq creates destinations on demand, there is no need to create
them up front.
using a producer to send to x, will cause x (queue or topic) to be created.

http://activemq.apache.org/how-do-i-create-new-destinations.html


On 2 January 2013 12:43, jainmanglesh <ja...@rediffmail.com> wrote:

> Hi,
> I agree with you however the only issue is I would be getting the number of
> queue dynamically from some other class and hence according to requirement
> I
> need to create queues dynamically in the program.
> I have code something like this:
>         public static void main(String[] args) throws Exception {
>                 listDestination = new ActiveMQAllProducer().createQueue();
>
>                 thread(new producerThread(), false);
>                 Thread.sleep(10000);
>                 thread(new consumerThread(), false);
>
>         }
>
>         public List<Destination> createQueue() {
>                 // Create a ConnectionFactory
>                 ActiveMQConnectionFactory connectionFactory = new
> ActiveMQConnectionFactory(url);
>
>                 List<Destination> listDestination = new
> ArrayList<Destination>();
>                 try {
>
>                         // Create a Connection
>                         Connection connection =
> connectionFactory.createConnection();
>                         connection.start();
>
>                         for(int i = 1; i < 11; i++){
>                                 // Create a Session
>                                 Session session =
> connection.createSession(true,
> Session.AUTO_ACKNOWLEDGE);
>
>                                 // Create the destination (Topic or Queue)
>                                 Destination destination =
> session.createQueue("myQueue" + i);
>                                 listDestination.add(destination);
>
>                         }
>                         connection.close();
>                 }
>                 catch (Exception e) {
>                         System.out.println("Caught: " + e);
>                         e.printStackTrace();
>                 }
>                 return listDestination;
>         }
>
>         public void QueueProducer(List<Destination> list){
>                 // Create a ConnectionFactory
>                 ActiveMQConnectionFactory connectionFactory = new
> ActiveMQConnectionFactory(url);
>
>                 try {
>
>                         // Create a Connection
>                         Connection connection =
> connectionFactory.createConnection();
>                         connection.start();
>
>                         // Create a Session
>                         Session session = connection.createSession(true,
> Session.AUTO_ACKNOWLEDGE);
>
>                         // Create a MessageProducer from the Session to
> the Topic or Queue
>                         for(int i = 0; i < list.size(); i++){
>                                 MessageProducer producer =
> session.createProducer(list.get(i));
>
>                                 // Create a messages
>                                 String text = "Hello world! From: " + i +
> " : " + this.hashCode();
>                                 TextMessage message =
> session.createTextMessage(text);
>
>                                 // Tell the producer to send the message
>                                 System.out.println("Sent message: "+
> message.hashCode() + " : " +
> "message " + i);
>                                 producer.send(message);
>                         }
>                         // Clean up
>                         session.close();
>                         connection.close();
>                 }
>                 catch (Exception e) {
>                         System.out.println("Caught: " + e);
>                         e.printStackTrace();
>                 }
>         }
>
>         public void QueueConsumer(List<Destination> list){
>                 // Create a ConnectionFactory
>                 ActiveMQConnectionFactory connectionFactory = new
> ActiveMQConnectionFactory(url);
>
>                 try {
>
>                         // Create a Connection
>                         Connection connection =
> connectionFactory.createConnection();
>                         connection.start();
>
>                         // Create a Session
>                         Session session = connection.createSession(true,
> Session.AUTO_ACKNOWLEDGE);
>
>                         // Create a MessageConsumer from the Session to
> the Topic or Queue
>                         for(int i = 0; i < list.size(); i++){
>                                 MessageConsumer consumer =
> session.createConsumer(list.get(i));
>
>                         // Wait for a message
>                         Message message = consumer.receive(1000);
>
>                         if (message instanceof TextMessage) {
>                                 TextMessage textMessage = (TextMessage)
> message;
>                                 String text = textMessage.getText();
>                                 System.out.println("Received: " + text);
>                         } else {
>                                 System.out.println("Received: " + message);
>                         }
>                         }
>                         // Clean up
>                         session.close();
>                         connection.close();
>                 }
>                 catch (Exception e) {
>                         System.out.println("Caught: " + e);
>                         e.printStackTrace();
>                 }
>         }
>
>
> However if I dont call producer in the main method then it wont create
> queue
> (as you mentioned).
> I cannot configure the queue in xml file as per my requirement of creating
> queue in java program (as I would come to know the number of queue to
> create
> at run time).
>
> bit stuck with the requirement and not able to move forward,
> Any help would be appreciated.
> Thank you very much
>
>
>
>
> --
> View this message in context:
> http://activemq.2283324.n4.nabble.com/How-to-setup-multiple-queues-tp3092113p4661199.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>



-- 
http://redhat.com
http://blog.garytully.com

Re: How to setup multiple queues

Posted by jainmanglesh <ja...@rediffmail.com>.
Hi, 
I agree with you however the only issue is I would be getting the number of
queue dynamically from some other class and hence according to requirement I
need to create queues dynamically in the program.
I have code something like this:
	public static void main(String[] args) throws Exception {
		listDestination = new ActiveMQAllProducer().createQueue();

		thread(new producerThread(), false);
		Thread.sleep(10000);
		thread(new consumerThread(), false);

	}

	public List<Destination> createQueue() {
		// Create a ConnectionFactory
		ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory(url);

		List<Destination> listDestination = new ArrayList<Destination>();
		try {

			// Create a Connection
			Connection connection = connectionFactory.createConnection();
			connection.start();

			for(int i = 1; i < 11; i++){
				// Create a Session
				Session session = connection.createSession(true,
Session.AUTO_ACKNOWLEDGE);

				// Create the destination (Topic or Queue)
				Destination destination = session.createQueue("myQueue" + i);
				listDestination.add(destination);

			}
			connection.close();
		}
		catch (Exception e) {
			System.out.println("Caught: " + e);
			e.printStackTrace();
		}
		return listDestination;
	}

	public void QueueProducer(List<Destination> list){
		// Create a ConnectionFactory
		ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory(url);

		try {

			// Create a Connection
			Connection connection = connectionFactory.createConnection();
			connection.start();

			// Create a Session
			Session session = connection.createSession(true,
Session.AUTO_ACKNOWLEDGE);

			// Create a MessageProducer from the Session to the Topic or Queue
			for(int i = 0; i < list.size(); i++){
				MessageProducer producer = session.createProducer(list.get(i));

				// Create a messages
				String text = "Hello world! From: " + i + " : " + this.hashCode();
				TextMessage message = session.createTextMessage(text);

				// Tell the producer to send the message
				System.out.println("Sent message: "+ message.hashCode() + " : " +
"message " + i);
				producer.send(message);
			}
			// Clean up
			session.close();
			connection.close();
		}
		catch (Exception e) {
			System.out.println("Caught: " + e);
			e.printStackTrace();
		}
	}

	public void QueueConsumer(List<Destination> list){
		// Create a ConnectionFactory
		ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory(url);

		try {

			// Create a Connection
			Connection connection = connectionFactory.createConnection();
			connection.start();

			// Create a Session
			Session session = connection.createSession(true,
Session.AUTO_ACKNOWLEDGE);

			// Create a MessageConsumer from the Session to the Topic or Queue
			for(int i = 0; i < list.size(); i++){
				MessageConsumer consumer = session.createConsumer(list.get(i));

			// Wait for a message
			Message message = consumer.receive(1000);

			if (message instanceof TextMessage) {
				TextMessage textMessage = (TextMessage) message;
				String text = textMessage.getText();
				System.out.println("Received: " + text);
			} else {
				System.out.println("Received: " + message);
			}
			}
			// Clean up
			session.close();
			connection.close();
		}
		catch (Exception e) {
			System.out.println("Caught: " + e);
			e.printStackTrace();
		}
	}


However if I dont call producer in the main method then it wont create queue
(as you mentioned).
I cannot configure the queue in xml file as per my requirement of creating
queue in java program (as I would come to know the number of queue to create
at run time).

bit stuck with the requirement and not able to move forward, 
Any help would be appreciated.
Thank you very much




--
View this message in context: http://activemq.2283324.n4.nabble.com/How-to-setup-multiple-queues-tp3092113p4661199.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: How to setup multiple queues

Posted by Christian Posta <ch...@gmail.com>.
You could configure the broker to create those queues on startup. That way
you don't need any separate for-loop and the queues will already be visible
via the admin console. See:

http://activemq.apache.org/configure-startup-destinations.html


On Wed, Jan 2, 2013 at 3:02 AM, jainmanglesh <ja...@rediffmail.com>wrote:

> Hi
> Thank you very much for your reply.
>
> my requirement is like:
> I want to create a list of activemq (which I am trying to create using for
> loop as in code). These queues will be used by third party to send message
> and on the other end used by third party to consume the messages on queue.
>
> Third party sending messages ----> q1, q2,q3... q10  ---> Third party
> consuming messages.
>
> A random queue will be selected by both third party to put and take the
> messages from queue.
>
> I cannot have a code to produce the message and put into queue (sorry about
> adding that in the code above).
> However as you said, If I dont have producer it does not create the queue
> (as I cannot see on admin console).
>
> The only thing is I want to create Queues, that's all. and third party will
> be using them to put and take messages from its ends.
>
> Thank you.
>
>
>
> --
> View this message in context:
> http://activemq.2283324.n4.nabble.com/How-to-setup-multiple-queues-tp3092113p4661197.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>



-- 
*Christian Posta*
http://www.christianposta.com/blog
twitter: @christianposta