Search in sources :

Example 16 with VCMessageSession

use of cbit.vcell.message.VCMessageSession in project vcell by virtualcell.

the class TestBlobMessages method testBlobMessages.

@Test
public void testBlobMessages() throws VCMessagingException {
    // creating one messageProducer session
    ArrayList<VCMessageSession> sessions = new ArrayList<VCMessageSession>();
    for (int i = 0; i < NUM_PRODUCERS; i++) {
        sessions.add(messagingService.createProducerSession());
    }
    AtomicInteger messagesToRead = new AtomicInteger(0);
    long startTime_MS = System.currentTimeMillis();
    final long TIMEOUT_MS = 1000 * 2;
    for (int i = 0; i < NUM_MESSAGES; i++) {
        for (int s = 0; s < NUM_PRODUCERS; s++) {
            VCMessageSession session = sessions.get(s);
            byte[] objectContent = new byte[40000 * (i + 1)];
            Arrays.fill(objectContent, (byte) 22);
            VCMessage message = session.createObjectMessage(objectContent);
            // VCMessage message = session.createObjectMessage(new byte[100000000]);
            session.sendQueueMessage(VCellTestQueue.JimQueue, message, false, 100000L);
            session.commit();
            int numMessages = messagesToRead.incrementAndGet();
            System.out.println("sent message:  messages unread = " + numMessages);
        }
    }
    // create N comsumers
    for (int i = 0; i < NUM_COMSUMERS; i++) {
        QueueListener listener = new QueueListener() {

            public void onQueueMessage(VCMessage vcMessage, VCMessageSession session) throws RollbackException {
                Object objectContent = vcMessage.getObjectContent();
                System.out.println("object content is '" + objectContent + "' of type " + objectContent.getClass().getName());
                String blobInfo = "<no blob>";
                if (vcMessage.propertyExists(VCMessageJms.BLOB_MESSAGE_MONGODB_OBJECTID)) {
                    String objectType = vcMessage.getStringProperty(VCMessageJms.BLOB_MESSAGE_OBJECT_TYPE);
                    int objectSize = vcMessage.getIntProperty(VCMessageJms.BLOB_MESSAGE_OBJECT_SIZE);
                    String objectid = vcMessage.getStringProperty(VCMessageJms.BLOB_MESSAGE_MONGODB_OBJECTID);
                    String persistenceType = vcMessage.getStringProperty(VCMessageJms.BLOB_MESSAGE_PERSISTENCE_TYPE);
                    blobInfo = "BLOB: persistenceType=" + persistenceType + ", object=" + objectType + ", size=" + objectSize + ", objectId=" + objectid;
                }
                System.out.println("<<<< onQueueMessage() >>>> timestampMS=" + vcMessage.getTimestampMS() + ", " + toString() + ",  elapsedTimeS=" + ((System.currentTimeMillis() - vcMessage.getTimestampMS()) / 1000.0) + ", Received: " + blobInfo);
                session.commit();
                int numMessages = messagesToRead.decrementAndGet();
                System.out.println("read message:  messages unread = " + numMessages);
            }
        };
        VCQueueConsumer queueConsumer = new VCQueueConsumer(VCellTestQueue.JimQueue, listener, null, "Queue[" + VCellTestQueue.JimQueue.getName() + "] ==== Consumer Thread " + i, 1);
        messagingService.addMessageConsumer(queueConsumer);
    }
    System.out.println("waiting for messages to be read");
    int numUnreadMessages = messagesToRead.get();
    while (numUnreadMessages > 0) {
        long elapsedTime = System.currentTimeMillis() - startTime_MS;
        System.out.println("elapsed time is " + elapsedTime + " ms, " + numUnreadMessages + " unread messages");
        if (elapsedTime > TIMEOUT_MS) {
            throw new RuntimeException("timeout before processing all messages, " + numUnreadMessages + " unread");
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        numUnreadMessages = messagesToRead.get();
    }
    System.out.println("done");
}
Also used : QueueListener(cbit.vcell.message.VCQueueConsumer.QueueListener) VCMessageSession(cbit.vcell.message.VCMessageSession) ArrayList(java.util.ArrayList) VCQueueConsumer(cbit.vcell.message.VCQueueConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) VCMessage(cbit.vcell.message.VCMessage) Test(org.junit.Test)

Example 17 with VCMessageSession

use of cbit.vcell.message.VCMessageSession 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.setDelegate(new SimpleMessagingDelegate());
        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)

Example 18 with VCMessageSession

use of cbit.vcell.message.VCMessageSession in project vcell by virtualcell.

the class SolverPreprocessor method sendFailureAndExit.

/**
 * Starts the application.
 * @param args an array of command-line arguments
 * @throws VCMessagingException
 */
public static void sendFailureAndExit(HTCSolver htcSolver, SimulationTask simTask, String hostName, SimulationMessage simMessage) throws VCMessagingException {
    VCMessagingService service = VCellServiceHelper.getInstance().loadService(VCMessagingService.class);
    service.setDelegate(new ServerMessagingDelegate());
    VCMessageSession session = service.createProducerSession();
    try {
        WorkerEventMessage.sendFailed(session, htcSolver, simTask, hostName, simMessage);
        sleep(500);
        service.close();
        sleep(500);
    } catch (VCMessagingException e1) {
        e1.printStackTrace();
    } finally {
        System.exit(-1);
    }
}
Also used : ServerMessagingDelegate(cbit.vcell.message.server.ServerMessagingDelegate) VCMessageSession(cbit.vcell.message.VCMessageSession) VCMessagingService(cbit.vcell.message.VCMessagingService) VCMessagingException(cbit.vcell.message.VCMessagingException)

Aggregations

VCMessageSession (cbit.vcell.message.VCMessageSession)18 KeyValue (org.vcell.util.document.KeyValue)9 VCMessage (cbit.vcell.message.VCMessage)8 VCMessagingException (cbit.vcell.message.VCMessagingException)7 User (org.vcell.util.document.User)7 VCQueueConsumer (cbit.vcell.message.VCQueueConsumer)6 BigString (org.vcell.util.BigString)6 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)6 UserLoginInfo (org.vcell.util.document.UserLoginInfo)6 VCMessagingService (cbit.vcell.message.VCMessagingService)5 SimulationRep (cbit.vcell.modeldb.SimulationRep)5 VCSimulationIdentifier (cbit.vcell.solver.VCSimulationIdentifier)5 XmlParseException (cbit.vcell.xml.XmlParseException)5 DataAccessException (org.vcell.util.DataAccessException)5 MappingException (cbit.vcell.mapping.MappingException)4 MathException (cbit.vcell.math.MathException)4 MatrixException (cbit.vcell.matrix.MatrixException)4 VCMessageSelector (cbit.vcell.message.VCMessageSelector)4 QueueListener (cbit.vcell.message.VCQueueConsumer.QueueListener)4 ModelException (cbit.vcell.model.ModelException)4