TopicConnectionFactory fact = (TopicConnectionFactory) namingContext.lookup("java:/ConnectionFactory");
connection = fact.createTopicConnection();
Topic topic = (Topic) namingContext.lookup("topic/metrics");
TopicSession session = connection.createTopicSession(IS_TRANSACTED, ACKNOWLEDGE_MODE);
TopicPublisher pub = session.createPublisher(topic);
pub.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
pub.setPriority(Message.DEFAULT_PRIORITY);
pub.setTimeToLive(Message.DEFAULT_TIME_TO_LIVE);
// start the JMS connection
connection.start();
// copy the message queue every x seconds, and publish the messages
while (running)
{
Object[] array;
long sleepTime = delay;
try
{
Thread.sleep(sleepTime);
// measure message processing cost and try to deal
// with congestion
long begin = System.currentTimeMillis();
// synchronized during the copy... the interceptor will
// have to wait til done
synchronized (msgQueue)
{
array = msgQueue.toArray();
msgQueue.clear();
}
// publish the messages
for (int i = 0; i < array.length; ++i)
{
Message msg = createMessage(session,
((Entry) array[i]).principal,
((Entry) array[i]).id,
((Entry) array[i]).method,
((Entry) array[i]).checkpoint,
((Entry) array[i]).time
);
pub.publish(msg);
}
// try to deal with congestion a little better, alot of
// small messages fast will kill JBossMQ performance, this is
// a temp fix to group many messages into one operation
try
{
session.commit();
}
catch (Exception e)
{
}