I have the following code so far:-
Producer code:-
public class TradeProducer implements Runnable {
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("vm://localhost");
// Create a Connection
javax.jms.Connection connection =
connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// Create the Topic
Topic topic = session.createTopic("prospective_trade");
// Create a MessageProducer from the Session to the Topic
MessageProducer producer = session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
String text = "Hello world! From: " +
Thread.currentThread().getName() + " : " + this.hashCode();
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Sent message: "+ message.hashCode() + "
: " + Thread.currentThread().getName());
producer.send(message);
// Clean up
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
}
Consumer code:-
public class TradeConsumer implements Runnable, ExceptionListener {
public void run() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new
ActiveMQConnectionFactory("vm://localhost");
// Create a Connection
javax.jms.Connection connection =
connectionFactory.createConnection();
connection.start();
connection.setExceptionListener(this);
// Create a Session
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
// Create the Topic
Topic topic = session.createTopic("prospective_trade");
// Create a MessageConsumer from the Session to the Topic
MessageConsumer consumer = session.createConsumer(topic);
// 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);
}
consumer.close();
session.close();
connection.close();
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
public synchronized void onException(JMSException ex) {
System.out.println("JMS Exception occurred. Shutting down
client.");
}
}
My question:-
Is it possible to enclose the "wait for a message' section of code in a loop
which has as a maximum number, the no. of active consumers minus 1 ?
i.e. I am trying to decide when I have received messages (one only) from
each of the other active consumers before continuing with the remainder of
the code
--
View this message in context:
http://activemq.2283324.n4.nabble.com/First-activeMQ-use-help-please-tp4703506.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.