如何监视多个JMS队列

时间:2015-07-01 13:46:27

标签: java multithreading queue jms

我的应用程序需要监视多个JMS队列。

应该怎么做? 开始2个主题? 可以同时监控2个队列吗?

一个队列的示例代码:

...
queue1 = session.createQueue("queue-1");
consumer = session.createConsumer(queue1);

connection.start();

while (true) {
    Message m = consumer.receive(10000);

    if (m == null) {
        ...nothing...
    } else {
        ...do something with the message...
    }
}
...

我应该如何看队列1和队列2?

2 个答案:

答案 0 :(得分:0)

您可以使用quartz scheduler Quartz Scheduler。实现一个(或多个)石英作业,如下所示:

public class MessageReaderJob1 implements Job {
private QueueReader1 qr;
@Override
public synchronized void execute(JobExecutionContext arg0) throws JobExecutionException {
    qr = QueueReader1.getInstance();

    try {
        Message message = qr.getConsumer().receiveNoWait();
        ....
    }
}

然后,您将需要一个将从应用程序(main方法或servlet)运行的调度程序,请注意您也可以为第二个队列实现不同的触发器:

public class TestCasesSchedule {

private Scheduler scheduler;

public void createSchedule() {
    JobDetail job1 = JobBuilder.newJob(MessageReaderJob1.class)
            .withIdentity("jobname", Scheduler.DEFAULT_GROUP)
            .build();

    JobDetail job2 = JobBuilder.newJob(MessageReaderJob2.class)
            .withIdentity("jobname", Scheduler.DEFAULT_GROUP)
            .build();

    Trigger trigger = TriggerBuilder.newTrigger()
            .withIdentity("minutestrigger", "triggergroup")
            .startNow()
            .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInMinutes(5)
                    .repeatForever())
            .build();

    try {
        SchedulerFactory sf = new StdSchedulerFactory();
        scheduler = sf.getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job1, trigger);
        scheduler.scheduleJob(job2, trigger);
    } catch (SchedulerException se) {
        System.err.println(se.getMessage())
    }
}

你的一个队列的QueueReader看起来像这样:

public class QueueReader1 {

private MessageConsumer consumer = null;
private Context jndiContext = null;
private QueueConnectionFactory queueConnectionFactory = null;
private QueueConnection queueConnection = null;
private QueueSession queueSession = null;
private Queue queue = null;

private static final QueueReader instance = new QueueReader();

public synchronized static QueueReader getInstance() {
    return instance;
}

private QueueReader() {

    /*
     * Create a JNDI API InitialContext object if none exists
     * yet.
     */
     try {
        jndiContext = new InitialContext();


    } catch (NamingException e) {
        System.err.println(e.getMessage())
        System.exit(1);
    }

    /*
     * Look up connection factory and queue.  If either does
     * not exist, exit.
     */
    try {
        queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("connection_factory_name");
        queue = (Queue) jndiContext.lookup("queue_name");
        queueConnection =
                queueConnectionFactory.createQueueConnection();
        queueSession =
                queueConnection.createQueueSession(false,
                Session.AUTO_ACKNOWLEDGE);

        consumer = queueSession.createConsumer(queue);

        queueConnection.start();

    } catch (JMSException ex) {
         System.err.println(ex.getMessage());
    } catch (NamingException e) {
         System.err.println(e.getMessage());
    }
}

}

答案 1 :(得分:0)

这是我的解决方案。有用。欢迎任何额外的建议!

主要课程:

$(document).ready(function(){
    $("button").click(function(){
        $("#mainDiv").load("index.jsp", function() {
          $('#regButton').remove();
        });
    });
});

线程:

public class Notifier {
    public static void main(String[] args) throws Exception {
        // Start a thread for each JMQ queue to monitor.
        DestinationThread destination1 = new DestinationThread("queue1");
        DestinationThread destination2 = new DestinationThread("queue2");
        destination1.start();
        destination2.start();
    }
}