Search in sources :

Example 61 with ObjectMessage

use of javax.jms.ObjectMessage in project ignite by apache.

the class IgniteJmsStreamerTest method testTopicFromName.

/**
 * @throws Exception If failed.
 */
public void testTopicFromName() throws JMSException, InterruptedException {
    Destination dest = new ActiveMQTopic(TOPIC_NAME);
    try (IgniteDataStreamer<String, String> dataStreamer = grid().dataStreamer(DEFAULT_CACHE_NAME)) {
        JmsStreamer<ObjectMessage, String, String> jmsStreamer = newJmsStreamer(ObjectMessage.class, dataStreamer);
        jmsStreamer.setDestinationType(Topic.class);
        jmsStreamer.setDestinationName(TOPIC_NAME);
        // subscribe to cache PUT events and return a countdown latch starting at CACHE_ENTRY_COUNT
        CountDownLatch latch = subscribeToPutEvents(CACHE_ENTRY_COUNT);
        jmsStreamer.start();
        // produce messages
        produceObjectMessages(dest, false);
        // all cache PUT events received in 10 seconds
        latch.await(10, TimeUnit.SECONDS);
        assertAllCacheEntriesLoaded();
        jmsStreamer.stop();
    }
}
Also used : Destination(javax.jms.Destination) ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) ObjectMessage(javax.jms.ObjectMessage) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 62 with ObjectMessage

use of javax.jms.ObjectMessage in project vcell by virtualcell.

the class MessageProducerSessionJms method sendRpcMessage.

// public MessageProducerSessionJms(Session session, VCMessagingServiceJms vcMessagingServiceJms) {
// System.out.println("-----\nmpjms MessageProducerSessionJms(Session session, VCMessagingServiceJms vcMessagingServiceJms)\ntmpQCnt="+(++tmpQCnt)+"----------");
// Thread.dumpStack();
// this.vcMessagingServiceJms = vcMessagingServiceJms;
// this.session = session;
// this.bIndependent = false;
// }
public /*synchronized*/
Object sendRpcMessage(VCellQueue queue, VCRpcRequest vcRpcRequest, boolean returnRequired, long timeoutMS, String[] specialProperties, Object[] specialValues, UserLoginInfo userLoginInfo) throws VCMessagingException, VCMessagingInvocationTargetException {
    MessageProducer messageProducer = null;
    MessageProducerSessionJms tempMessageProducerSessionJms = null;
    try {
        if (!bIndependent) {
            throw new VCMessagingException("cannot invoke RpcMessage from within another transaction, create an independent message producer");
        }
        Destination destination = session.createQueue(queue.getName());
        messageProducer = session.createProducer(destination);
        // 
        // use MessageProducerSessionJms to create the rpcRequest message (allows "Blob" messages to be formed as needed).
        // 
        tempMessageProducerSessionJms = new MessageProducerSessionJms(vcMessagingServiceJms);
        // tempMessageProducerSessionJms = new MessageProducerSessionJms(session,vcMessagingServiceJms);
        VCMessageJms vcRpcRequestMessage = (VCMessageJms) tempMessageProducerSessionJms.createObjectMessage(vcRpcRequest);
        Message rpcMessage = vcRpcRequestMessage.getJmsMessage();
        rpcMessage.setStringProperty(VCMessagingConstants.MESSAGE_TYPE_PROPERTY, VCMessagingConstants.MESSAGE_TYPE_RPC_SERVICE_VALUE);
        rpcMessage.setStringProperty(VCMessagingConstants.SERVICE_TYPE_PROPERTY, vcRpcRequest.getRequestedServiceType().getName());
        // rpcMessage.setJMSExpiration(5000);
        if (specialValues != null) {
            for (int i = 0; i < specialValues.length; i++) {
                rpcMessage.setObjectProperty(specialProperties[i], specialValues[i]);
            }
        }
        if (returnRequired) {
            rpcMessage.setJMSReplyTo(commonTemporaryQueue);
            messageProducer.setTimeToLive(timeoutMS);
            messageProducer.send(rpcMessage);
            session.commit();
            vcMessagingServiceJms.getDelegate().onRpcRequestSent(vcRpcRequest, userLoginInfo, vcRpcRequestMessage);
            System.out.println("MessageProducerSessionJms.sendRpcMessage(): looking for reply message with correlationID = " + rpcMessage.getJMSMessageID());
            String filter = VCMessagingConstants.JMSCORRELATIONID_PROPERTY + "='" + rpcMessage.getJMSMessageID() + "'";
            MessageConsumer replyConsumer = null;
            Message replyMessage = null;
            try {
                replyConsumer = session.createConsumer(commonTemporaryQueue, filter);
                replyMessage = replyConsumer.receive(timeoutMS);
            } finally {
                replyConsumer.close();
            }
            if (replyMessage == null) {
                System.out.println("Request timed out");
            }
            if (replyMessage == null || !(replyMessage instanceof ObjectMessage)) {
                throw new JMSException("Server is temporarily not responding, please try again. If problem persists, contact VCell_Support@uchc.edu." + " (server " + vcRpcRequest.getRequestedServiceType().getName() + ", method " + vcRpcRequest.getMethodName() + ")");
            } else {
                VCMessageJms vcReplyMessage = new VCMessageJms(replyMessage, vcMessagingServiceJms.getDelegate());
                vcReplyMessage.loadBlobFile();
                Object returnValue = vcReplyMessage.getObjectContent();
                vcReplyMessage.removeBlobFile();
                if (returnValue instanceof Exception) {
                    throw new VCMessagingInvocationTargetException((Exception) returnValue);
                } else {
                    return returnValue;
                }
            }
        } else {
            rpcMessage.setJMSReplyTo(commonTemporaryQueue);
            messageProducer.setTimeToLive(timeoutMS);
            messageProducer.send(rpcMessage);
            commit();
            vcMessagingServiceJms.getDelegate().onRpcRequestSent(vcRpcRequest, userLoginInfo, vcRpcRequestMessage);
            return null;
        }
    } catch (JMSException e) {
        onException(e);
        throw new VCMessagingException(e.getMessage(), e);
    } finally {
        try {
            if (tempMessageProducerSessionJms != null) {
                tempMessageProducerSessionJms.commit();
                tempMessageProducerSessionJms.close();
            }
            if (messageProducer != null) {
                messageProducer.close();
            }
        // try{Thread.sleep(10000);}catch(Exception e){e.printStackTrace();}
        } catch (JMSException e) {
            onException(e);
        }
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) VCMessagingInvocationTargetException(cbit.vcell.message.VCMessagingInvocationTargetException) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) VCMessage(cbit.vcell.message.VCMessage) JMSException(javax.jms.JMSException) VCMessagingException(cbit.vcell.message.VCMessagingException) VCMessagingInvocationTargetException(cbit.vcell.message.VCMessagingInvocationTargetException) JMSException(javax.jms.JMSException) ObjectMessage(javax.jms.ObjectMessage) MessageProducer(javax.jms.MessageProducer) VCMessagingException(cbit.vcell.message.VCMessagingException)

Example 63 with ObjectMessage

use of javax.jms.ObjectMessage in project vcell by virtualcell.

the class MessageProducerSessionJms method createObjectMessage.

public VCMessage createObjectMessage(Serializable object) {
    try {
        // if the serialized object is very large, send it as a BlobMessage (ActiveMQ specific).
        long t1 = System.currentTimeMillis();
        byte[] serializedBytes = null;
        if (object != null) {
            serializedBytes = BeanUtils.toSerialized(object);
        }
        long blobMessageSizeThreshold = Long.parseLong(PropertyLoader.getProperty(PropertyLoader.jmsBlobMessageMinSize, "100000"));
        boolean USE_MONGO = Boolean.parseBoolean(PropertyLoader.getRequiredProperty(PropertyLoader.jmsBlobMessageUseMongo));
        if (serializedBytes != null && serializedBytes.length > blobMessageSizeThreshold) {
            if (!USE_MONGO) {
                // 
                // get (or create) directory to store Message BLOBs
                // 
                File tempdir = new File(PropertyLoader.getRequiredProperty(PropertyLoader.jmsBlobMessageTempDir));
                if (!tempdir.exists()) {
                    tempdir.mkdirs();
                }
                // 
                // write serialized message to "temp" file.
                // 
                File blobFile = File.createTempFile("BlobMessage", ".data", tempdir);
                FileOutputStream fileOutputStream = new FileOutputStream(blobFile);
                FileChannel channel = fileOutputStream.getChannel();
                channel.write(ByteBuffer.wrap(serializedBytes));
                channel.close();
                fileOutputStream.close();
                ObjectMessage objectMessage = session.createObjectMessage("emptyObject");
                objectMessage.setStringProperty(VCMessageJms.BLOB_MESSAGE_PERSISTENCE_TYPE, VCMessageJms.BLOB_MESSAGE_PERSISTENCE_TYPE_FILE);
                objectMessage.setStringProperty(VCMessageJms.BLOB_MESSAGE_PRODUCER_TEMPDIR, tempdir.getAbsolutePath());
                objectMessage.setStringProperty(VCMessageJms.BLOB_MESSAGE_FILE_NAME, blobFile.getName());
                objectMessage.setStringProperty(VCMessageJms.BLOB_MESSAGE_OBJECT_TYPE, object.getClass().getName());
                objectMessage.setIntProperty(VCMessageJms.BLOB_MESSAGE_OBJECT_SIZE, serializedBytes.length);
                vcMessagingServiceJms.getDelegate().onTraceEvent("MessageProducerSessionJms.createObjectMessage: (BLOB) size=" + serializedBytes.length + ", type=" + object.getClass().getName() + ", elapsedTime = " + (System.currentTimeMillis() - t1) + " ms");
                return new VCMessageJms(objectMessage, object, vcMessagingServiceJms.getDelegate());
            } else {
                String hexString = Long.toHexString(Math.abs(new Random().nextLong()));
                ObjectId objectId = VCMongoDbDriver.getInstance().storeBLOB("jmsblob_name_" + hexString, "jmsblob", serializedBytes);
                ObjectMessage objectMessage = session.createObjectMessage("emptyObject");
                objectMessage.setStringProperty(VCMessageJms.BLOB_MESSAGE_PERSISTENCE_TYPE, VCMessageJms.BLOB_MESSAGE_PERSISTENCE_TYPE_MONGODB);
                objectMessage.setStringProperty(VCMessageJms.BLOB_MESSAGE_MONGODB_OBJECTID, objectId.toHexString());
                objectMessage.setStringProperty(VCMessageJms.BLOB_MESSAGE_OBJECT_TYPE, object.getClass().getName());
                objectMessage.setIntProperty(VCMessageJms.BLOB_MESSAGE_OBJECT_SIZE, serializedBytes.length);
                vcMessagingServiceJms.getDelegate().onTraceEvent("MessageProducerSessionJms.createObjectMessage: (BLOB) size=" + serializedBytes.length + ", type=" + object.getClass().getName() + ", elapsedTime = " + (System.currentTimeMillis() - t1) + " ms");
                return new VCMessageJms(objectMessage, object, vcMessagingServiceJms.getDelegate());
            }
        } else {
            ObjectMessage objectMessage = (ObjectMessage) session.createObjectMessage(object);
            int size = (serializedBytes != null) ? (serializedBytes.length) : (0);
            String objectType = (serializedBytes != null) ? (object.getClass().getName()) : ("NULL");
            vcMessagingServiceJms.getDelegate().onTraceEvent("MessageProducerSessionJms.createObjectMessage: (NOBLOB) size=" + size + ", type=" + objectType + ", elapsedTime = " + (System.currentTimeMillis() - t1) + " ms");
            return new VCMessageJms(objectMessage, vcMessagingServiceJms.getDelegate());
        }
    } catch (JMSException e) {
        e.printStackTrace(System.out);
        onException(e);
        throw new RuntimeException("unable to create object message", e);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : ObjectId(org.bson.types.ObjectId) FileChannel(java.nio.channels.FileChannel) JMSException(javax.jms.JMSException) VCMessagingException(cbit.vcell.message.VCMessagingException) VCMessagingInvocationTargetException(cbit.vcell.message.VCMessagingInvocationTargetException) JMSException(javax.jms.JMSException) Random(java.util.Random) ObjectMessage(javax.jms.ObjectMessage) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 64 with ObjectMessage

use of javax.jms.ObjectMessage in project vcell by virtualcell.

the class VCMessageJms method loadBlobFile.

/**
 * see Property jmsBlobMessageMinSize "vcell.jms.blobMessageMinSize"
 * see Property jmsBlobMessageTempDir "vcell.jms.blobMessageTempDir"
 * see class MessageProducerSessionJms
 *
 * 1) Message Producer serializes into byte[] and compares size with PropertyLoader.jmsBlobMessageMinSize.
 * 2) For Large Object Messages (> threshold bytes), MessageProducerSessionJms writes bytes to a local file (e.g. MyBlobMessageTempDir/BlobMessage2295645974283237270.data).
 * 3) Pass file name as message properties so that receiver can delete file when done.
 * 4) consumer-side VCMessage infrastructure receives BlobMessage and invokes VCMessageJms.loadBlobMessage()
 * 5) loadBlobMessage() reads object from stream and attempts to delete both original and broker files.
 * 6) consumer's message listener calls getObjectContent() not knowing if it was sent as a Blob or not.
 * 7) message consumer calls VCMessageJms.removeBlobFile() to clean up disk.
 */
public void loadBlobFile() {
    if (blobObject != null) {
        return;
    }
    if (jmsMessage instanceof ObjectMessage && propertyExists(BLOB_MESSAGE_PERSISTENCE_TYPE) && getStringProperty(BLOB_MESSAGE_PERSISTENCE_TYPE).equals(BLOB_MESSAGE_PERSISTENCE_TYPE_FILE)) {
        try {
            long t1 = System.currentTimeMillis();
            // 
            // read serialized object from inputStream (from Broker's data file)
            // 
            String blobFileName = jmsMessage.getStringProperty(BLOB_MESSAGE_FILE_NAME);
            // 
            // get directory to retrieve Message BLOBs
            // 
            File localBlobDir = new File(PropertyLoader.getRequiredProperty(PropertyLoader.jmsBlobMessageTempDir));
            File producerBlobDir = new File(getStringProperty(BLOB_MESSAGE_PRODUCER_TEMPDIR));
            blobFile = new File(localBlobDir, blobFileName);
            if (!blobFile.exists()) {
                blobFile = new File(producerBlobDir, blobFileName);
                if (!blobFile.exists()) {
                    throw new RuntimeException("Message BLOB file \"" + blobFileName + "\" not found local=\"" + localBlobDir + "\" or producer=\"" + producerBlobDir + "\"");
                }
            }
            FileInputStream fis = new FileInputStream(blobFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            ObjectInputStream ois = new ObjectInputStream(bis);
            blobObject = (Serializable) ois.readObject();
            ois.close();
            bis.close();
            fis.close();
            delegate.onTraceEvent("VCMessageJms.loadBlobFile(): size=" + jmsMessage.getIntProperty(BLOB_MESSAGE_OBJECT_SIZE) + ", type=" + jmsMessage.getStringProperty(BLOB_MESSAGE_OBJECT_TYPE) + ", elapsedTime = " + (System.currentTimeMillis() - t1) + " ms");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    if (jmsMessage instanceof ObjectMessage && propertyExists(BLOB_MESSAGE_PERSISTENCE_TYPE) && getStringProperty(BLOB_MESSAGE_PERSISTENCE_TYPE).equals(BLOB_MESSAGE_PERSISTENCE_TYPE_MONGODB)) {
        try {
            long t1 = System.currentTimeMillis();
            // 
            // read serialized object from inputStream (from Broker's data file)
            // 
            String mongo_objectid_hex = jmsMessage.getStringProperty(BLOB_MESSAGE_MONGODB_OBJECTID);
            blobObjectId = new ObjectId(mongo_objectid_hex);
            byte[] blob = VCMongoDbDriver.getInstance().getBLOB(blobObjectId);
            ByteArrayInputStream bis = new ByteArrayInputStream(blob);
            ObjectInputStream ois = new ObjectInputStream(bis);
            blobObject = (Serializable) ois.readObject();
            ois.close();
            bis.close();
            delegate.onTraceEvent("VCMessageJms.loadBlobFile(): size=" + jmsMessage.getIntProperty(BLOB_MESSAGE_OBJECT_SIZE) + ", type=" + jmsMessage.getStringProperty(BLOB_MESSAGE_OBJECT_TYPE) + ", elapsedTime = " + (System.currentTimeMillis() - t1) + " ms");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ObjectId(org.bson.types.ObjectId) ByteArrayInputStream(org.fusesource.hawtbuf.ByteArrayInputStream) ObjectMessage(javax.jms.ObjectMessage) File(java.io.File) FileInputStream(java.io.FileInputStream) JMSException(javax.jms.JMSException) MessagePropertyNotFoundException(cbit.vcell.message.MessagePropertyNotFoundException) ObjectInputStream(java.io.ObjectInputStream)

Example 65 with ObjectMessage

use of javax.jms.ObjectMessage in project tech by ffyyhh995511.

the class RequestProcessor method main.

public static void main(String[] args) throws Exception {
    RequestProcessor processor = new RequestProcessor();
    processor.init();
    while (true) {
        ObjectMessage message = (ObjectMessage) consumer.receive(1000);
        if (null != message) {
            System.out.println("message receive.....");
            System.out.println(message);
            HashMap<Serializable, Serializable> requestParam = (HashMap<Serializable, Serializable>) message.getObject();
            processor.requestHandler(requestParam);
        } else {
            break;
        }
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ObjectMessage(javax.jms.ObjectMessage)

Aggregations

ObjectMessage (javax.jms.ObjectMessage)71 JMSException (javax.jms.JMSException)29 Message (javax.jms.Message)24 Session (javax.jms.Session)18 Test (org.junit.Test)18 TextMessage (javax.jms.TextMessage)14 Serializable (java.io.Serializable)11 Destination (javax.jms.Destination)11 Map (java.util.Map)10 BytesMessage (javax.jms.BytesMessage)10 MapMessage (javax.jms.MapMessage)9 MessageProducer (javax.jms.MessageProducer)8 MessageConsumer (javax.jms.MessageConsumer)7 TreeMap (java.util.TreeMap)6 MessageCreator (org.springframework.jms.core.MessageCreator)5 DataNode (com.odysseusinc.arachne.portal.model.DataNode)4 HashMap (java.util.HashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 RequestMessage (com.alliander.osgp.shared.infra.jms.RequestMessage)3 ConsumerTemplate (com.odysseusinc.arachne.commons.service.messaging.ConsumerTemplate)3