Search in sources :

Example 1 with VCMessagingServiceEmbedded

use of cbit.vcell.message.jms.activeMQ.VCMessagingServiceEmbedded in project vcell by virtualcell.

the class TestQueues method main.

public static void main(String[] args) throws Exception {
    try {
        PropertyLoader.loadProperties();
        VCMessagingService messagingService = new VCMessagingServiceEmbedded();
        messagingService.setConfiguration(new SimpleMessagingDelegate(), null, -1);
        final Calculator calculator = new Calculator();
        // creating one messageProducer session
        ArrayList<VCMessageSession> sessions = new ArrayList<VCMessageSession>();
        for (int i = 0; i < NUM_PRODUCERS; i++) {
            sessions.add(messagingService.createProducerSession());
        }
        int sum = 0;
        for (int i = 0; i < NUM_MESSAGES; i++) {
            for (int s = 0; s < NUM_PRODUCERS; s++) {
                sum++;
                VCMessageSession session = sessions.get(s);
                VCMessage message = session.createTextMessage("message " + i + " from session " + s);
                session.sendQueueMessage(VCellTestQueue.JimQueue, message, false, 100000L);
                session.commit();
            }
            Thread.sleep(2000);
        }
        System.out.println("Correct sum is " + sum);
        Thread.sleep(30);
        // create N comsumers
        for (int i = 0; i < NUM_COMSUMERS; i++) {
            QueueListener listener = new QueueListener() {

                public void onQueueMessage(VCMessage vcMessage, VCMessageSession session) throws RollbackException {
                    // new Thread()
                    System.out.println("timestampMS=" + vcMessage.getTimestampMS() + ", " + toString() + ",  elapsedTimeS=" + ((System.currentTimeMillis() - vcMessage.getTimestampMS()) / 1000.0) + ", Received: " + vcMessage.getTextContent());
                    // int number = Integer.parseInt(vcMessage.getTextContent());
                    calculator.add(1);
                }
            };
            VCQueueConsumer queueConsumer = new VCQueueConsumer(VCellTestQueue.JimQueue, listener, null, "Queue[" + VCellTestQueue.JimQueue.getName() + "] ==== Consumer Thread " + i, 1);
            messagingService.addMessageConsumer(queueConsumer);
        }
        while (calculator.getSum() < sum) {
            System.out.println("calculator sum = " + calculator.getSum());
            Thread.sleep(1000);
        }
        System.out.println("calculator sum = " + calculator.getSum());
        System.out.println("main program calling closeAll()");
        messagingService.close();
        System.out.println("main program exiting");
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
}
Also used : SimpleMessagingDelegate(cbit.vcell.message.SimpleMessagingDelegate) QueueListener(cbit.vcell.message.VCQueueConsumer.QueueListener) VCMessage(cbit.vcell.message.VCMessage) VCMessageSession(cbit.vcell.message.VCMessageSession) VCMessagingServiceEmbedded(cbit.vcell.message.jms.activeMQ.VCMessagingServiceEmbedded) ArrayList(java.util.ArrayList) VCMessagingService(cbit.vcell.message.VCMessagingService) RollbackException(cbit.vcell.message.RollbackException) VCQueueConsumer(cbit.vcell.message.VCQueueConsumer)

Aggregations

RollbackException (cbit.vcell.message.RollbackException)1 SimpleMessagingDelegate (cbit.vcell.message.SimpleMessagingDelegate)1 VCMessage (cbit.vcell.message.VCMessage)1 VCMessageSession (cbit.vcell.message.VCMessageSession)1 VCMessagingService (cbit.vcell.message.VCMessagingService)1 VCQueueConsumer (cbit.vcell.message.VCQueueConsumer)1 QueueListener (cbit.vcell.message.VCQueueConsumer.QueueListener)1 VCMessagingServiceEmbedded (cbit.vcell.message.jms.activeMQ.VCMessagingServiceEmbedded)1 ArrayList (java.util.ArrayList)1