activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jainmanglesh <jainmangl...@rediffmail.com>
Subject Re: How to setup multiple queues
Date Wed, 02 Jan 2013 12:43:04 GMT
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.

Mime
View raw message