activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From patzerbud <patzer...@hotmail.com>
Subject Re: Network of Brokers
Date Sat, 17 Apr 2010 13:54:22 GMT



dkfn wrote:
> 
> It fails at:
> 
>            assertNotNull(message1);
> 
> btw.
> 

I think your example exhibits a threading issue. I modified it so the
consumer is in a separate thread like this:

	private class Consumer implements Runnable {
		Consumer() {

		}

		public void run() {

			BrokerService brokerService1 = null;
			Connection connection1 = null;

			try {
				brokerService1 = new BrokerService();
				brokerService1.setBrokerName("one");
				brokerService1.setUseJmx(false);
				brokerService1.setPersistenceAdapter(new MemoryPersistenceAdapter());
				brokerService1.addConnector("tcp://0.0.0.0:61616");
				NetworkConnector network1 =
brokerService1.addNetworkConnector("static:(tcp://localhost:51515)");
				network1.setName("network1");
				network1.setDynamicOnly(true);
				network1.setNetworkTTL(3);
				network1.setPrefetchSize(1);
				brokerService1.start();

				ActiveMQConnectionFactory connectionFactory1 = new
ActiveMQConnectionFactory("failover:(tcp://localhost:61616,tcp://localhost:51515)?randomize=false");
				connection1 = connectionFactory1.createConnection();
				connection1.start();

				Session session1 = connection1.createSession(true,
Session.AUTO_ACKNOWLEDGE);
				MessageConsumer consumer1 = session1.createConsumer(new
ActiveMQQueue("testingqueue"));

				for (int i=0; i<1; i++) {
					Message message1 = consumer1.receive();
					assertNotNull(message1);
					System.out.println(message1);
				}
			}
			catch (Exception e) {

			}
			finally {
				try {
					if (connection1 != null) {
						connection1.stop();
					}
				} catch (Throwable t) {
					t.printStackTrace();
				}

				try {
					if (brokerService1 != null) {
						brokerService1.stop();
					}
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}



		}
	}

Then insert this code before creating the producer & the message:

			try {
				Consumer consumer = new Consumer();
				thread = new Thread(consumer);
				thread.start();
			} catch (Exception ignoreMe) {
				ignoreMe.printStackTrace();
			}

Obviously, you will need to remove broker1 and all related stuff from your
test method (since it was moved to the private inner class Consumer) - but,
doing it this way will allow your test case to pass.

HTH,

Mike L (aka patzerbud)

-- 
View this message in context: http://old.nabble.com/Network-of-Brokers-tp28269405p28276280.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.


Mime
View raw message