To run the consumer, type ant consumer at the command
prompt. By default, the consumer consumes the published messages from a
queue.
The consumer Ant target points to the
consumerTool.java class, which uses the
ActiveMQConnectionFactory class to connect to the broker,
as shown:
Example 4.1. Connecting to a Broker
public class ConsumerTool {
...
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
...
// Create the connection.
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
connection = connectionFactory.createConnection();
connection.start();
...
}
The consumer uses createQueue()to create a queue or
createTopic()depending on which arguments you pass in
at runtime. The queue or topic name is defined as TEST.FOO in the
build.xml.
Example 4.2. Creating a Queue or Topic
...
private Session session;
private String subject = "TOOL.DEFAULT";
private boolean transacted;
...
// Create the session
Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
if (topic) {
destination = session.createTopic(subject);
} else {
destination = session.createQueue(subject);
}