Session producerSession = null;
Queue producerQueue = null;
MessageProducer producer;
MessageConsumer consumer;
Session consumerSession = null;
TemporaryQueue consumerDestination = null;
long startTime = 0;
// Check if JMX server port number was specified
jmxPort = System.getProperty("activemq.broker.jmx.port");
if ( jmxPort == null || jmxPort.trim().length() == 0 ) {
jmxPort = "1099"; // default
}
try {
// First create connection to a broker
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(brokerURI);
connection = factory.createConnection();
connection.start();
Runtime.getRuntime().addShutdownHook(
new Thread(
new Runnable() {
public void run() {
try {
if ( connection != null ) {
connection.close();
}
if ( jmxc != null ) {
jmxc.close();
}
} catch( Exception ex) {
}
}
}
)
);
URI target = new URI(brokerURI);
String brokerHost = target.getHost();
attachToRemoteBrokerJMXServer(brokerURI);
if (isQueueAvailable(queueName) == QueueState.exists) {
System.out.println("Queue "+queueName+" found on "+ brokerURI);
System.out.println("Sending getMeta...");
}
else if (isQueueAvailable(queueName) == QueueState.existsnot) {
System.err.println("Queue "+queueName+" does not exist on "+ brokerURI);
System.exit(1);
}
else {
System.out.println("Cannot see queues on JMX port "+brokerHost+":"+jmxPort);
System.out.println("Sending getMeta anyway...");
}
producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producerQueue = producerSession.createQueue(queueName);
producer = producerSession.createProducer(producerQueue);
consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumerDestination = consumerSession.createTemporaryQueue();
// -----------------------------------------------------------------------------
// Create message consumer. The consumer uses a selector to filter out messages other
// then GetMeta replies. Currently UIMA AS service returns two messages for each request:
// ServiceInfo message and GetMeta message. The ServiceInfo message is returned by the
// service immediately upon receiving a message from a client. This serves dual purpose,
// 1) to make sure the client reply destination exists
// 2) informs the client which service is processing its request
// -----------------------------------------------------------------------------
consumer = consumerSession.createConsumer(consumerDestination, "Command=2001");
TextMessage msg = producerSession.createTextMessage();
msg.setStringProperty(AsynchAEMessage.MessageFrom, consumerDestination.getQueueName());
msg.setStringProperty(UIMAMessage.ServerURI, brokerURI);
msg.setIntProperty(AsynchAEMessage.MessageType, AsynchAEMessage.Request);
msg.setIntProperty(AsynchAEMessage.Command, AsynchAEMessage.GetMeta);
msg.setJMSReplyTo(consumerDestination);
msg.setText("");