List of usage examples for javax.jms ObjectMessage getObject
Serializable getObject() throws JMSException;
From source file:org.dawnsci.commandserver.mx.example.ActiveMQConsumer.java
public static void main(String[] args) throws Exception { ConnectionFactory connectionFactory = ConnectionFactoryFacade .createConnectionFactory("tcp://sci-serv5.diamond.ac.uk:61616"); Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("testQ"); final MessageConsumer consumer = session.createConsumer(queue); connection.start();/*from w ww . j a v a 2 s. co m*/ while (true) { // You have to kill it to stop it! Message m = consumer.receive(1000); if (m != null) { if (m instanceof TextMessage) { TextMessage t = (TextMessage) m; System.out.println(t.getText()); try { ObjectMapper mapper = new ObjectMapper(); final SweepBean colBack = mapper.readValue(t.getText(), SweepBean.class); System.out.println("Data collection found: " + colBack.getDataCollectionId()); } catch (Exception ne) { System.out.println(m + " is not a data collection."); } } else if (m instanceof ObjectMessage) { ObjectMessage o = (ObjectMessage) m; System.out.println(o.getObject()); } } } }
From source file:org.dawnsci.commandserver.mx.example.ActiveMQProducer.java
public static void main(String[] args) throws Exception { QueueConnectionFactory connectionFactory = ConnectionFactoryFacade .createConnectionFactory("tcp://ws097.diamond.ac.uk:61616"); Connection send = connectionFactory.createConnection(); Session session = send.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue("testQ"); final MessageProducer producer = session.createProducer(queue); producer.setDeliveryMode(DeliveryMode.PERSISTENT); Message message = session.createTextMessage("Hello World"); producer.send(message);/*from www . j a v a 2 s . co m*/ message = session.createTextMessage("...and another message"); producer.send(message); message = session.createObjectMessage(new TestObjectBean("this could be", "anything")); producer.send(message); // Test JSON SweepBean col = new SweepBean("fred", "d0000000001", 0, 100); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(col); message = session.createTextMessage(jsonString); producer.send(message); producer.close(); session.close(); send.close(); // Now we peak at the queue // If the consumer is not going, the messages should still be there if (REQUIRE_PEAK) { QueueConnection qCon = connectionFactory.createQueueConnection(); QueueSession qSes = qCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); queue = qSes.createQueue("testQ"); qCon.start(); QueueBrowser qb = qSes.createBrowser(queue); Enumeration e = qb.getEnumeration(); if (e.hasMoreElements()) System.out.println("Peak at queue:"); while (e.hasMoreElements()) { Message m = (Message) e.nextElement(); if (m == null) continue; if (m instanceof TextMessage) { TextMessage t = (TextMessage) m; System.out.println(t.getText()); } else if (m instanceof ObjectMessage) { ObjectMessage o = (ObjectMessage) m; System.out.println(o.getObject()); } } qb.close(); qSes.close(); qCon.close(); } }
From source file:com.espertech.esperio.regression.adapter.SupportJMSReceiver.java
public static void print(Message msg) throws JMSException { log.info(".print received message: " + msg.getJMSMessageID()); if (msg instanceof ObjectMessage) { ObjectMessage objMsg = (ObjectMessage) msg; log.info(".print object: " + objMsg.getObject().toString()); } else {//from w w w .j av a2 s . co m MapMessage mapMsg = (MapMessage) msg; HashMap map = new HashMap(); Enumeration en = mapMsg.getMapNames(); while (en.hasMoreElements()) { String property = (String) en.nextElement(); Object mapObject = mapMsg.getObject(property); map.put(property, mapObject); } log.info(".print map: " + map); } }
From source file:com.mirth.connect.connectors.jms.JmsMessageUtils.java
/** * @param message/*w w w. j a v a 2 s.c o m*/ * the message to receive the bytes from. Note this only works * for TextMessge, ObjectMessage, StreamMessage and BytesMessage. * @return a byte array corresponding with the message payload * @throws JMSException * if the message can't be read or if the message passed is a * MapMessage * @throws java.io.IOException * if a failiare occurs while stream and converting the message * data */ public static byte[] getBytesFromMessage(Message message) throws JMSException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 2]; int len; if (message instanceof BytesMessage) { BytesMessage bMsg = (BytesMessage) message; // put message in read-only mode bMsg.reset(); while ((len = bMsg.readBytes(buffer)) != -1) { baos.write(buffer, 0, len); } } else if (message instanceof StreamMessage) { StreamMessage sMsg = (StreamMessage) message; sMsg.reset(); while ((len = sMsg.readBytes(buffer)) != -1) { baos.write(buffer, 0, len); } } else if (message instanceof ObjectMessage) { ObjectMessage oMsg = (ObjectMessage) message; ByteArrayOutputStream bs = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(bs); os.writeObject(oMsg.getObject()); os.flush(); baos.write(bs.toByteArray()); os.close(); bs.close(); } else if (message instanceof TextMessage) { TextMessage tMsg = (TextMessage) message; baos.write(tMsg.getText().getBytes()); } else { throw new JMSException("Cannot get bytes from Map Message"); } baos.flush(); byte[] bytes = baos.toByteArray(); baos.close(); return bytes; }
From source file:com.cws.esolutions.core.utils.MQUtils.java
/** * Gets an MQ message off a specified queue and returns it as an * <code>Object</code> to the requestor for further processing. * * @param connName - The connection name to utilize * @param authData - The authentication data to utilize, if required * @param responseQueue - The request queue name to put the message on * @param timeout - How long to wait for a connection or response * @param messageId - The JMS correlation ID of the message the response is associated with * @return <code>Object</code> - The serializable data returned by the MQ request * @throws UtilityException {@link com.cws.esolutions.core.utils.exception.UtilityException} if an error occurs processing *///w w w . ja va2s . c o m public static final synchronized Object getMqMessage(final String connName, final List<String> authData, final String responseQueue, final long timeout, final String messageId) throws UtilityException { final String methodName = MQUtils.CNAME + "getMqMessage(final String connName, final List<String> authData, final String responseQueue, final long timeout, final String messageId) throws UtilityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", connName); DEBUGGER.debug("Value: {}", responseQueue); DEBUGGER.debug("Value: {}", timeout); DEBUGGER.debug("Value: {}", messageId); } Connection conn = null; Session session = null; Object response = null; Context envContext = null; MessageConsumer consumer = null; ConnectionFactory connFactory = null; try { try { InitialContext initCtx = new InitialContext(); envContext = (Context) initCtx.lookup(MQUtils.INIT_CONTEXT); connFactory = (ConnectionFactory) envContext.lookup(connName); } catch (NamingException nx) { // we're probably not in a container connFactory = new ActiveMQConnectionFactory(connName); } if (DEBUG) { DEBUGGER.debug("ConnectionFactory: {}", connFactory); } if (connFactory == null) { throw new UtilityException("Unable to create connection factory for provided name"); } // Create a Connection conn = connFactory.createConnection(authData.get(0), PasswordUtils.decryptText(authData.get(1), authData.get(2), authData.get(3), Integer.parseInt(authData.get(4)), Integer.parseInt(authData.get(5)), authData.get(6), authData.get(7), authData.get(8))); conn.start(); if (DEBUG) { DEBUGGER.debug("Connection: {}", conn); } // Create a Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); if (DEBUG) { DEBUGGER.debug("Session: {}", session); } if (envContext != null) { try { consumer = session.createConsumer((Destination) envContext.lookup(responseQueue), "JMSCorrelationID='" + messageId + "'"); } catch (NamingException nx) { throw new UtilityException(nx.getMessage(), nx); } } else { Destination destination = session.createQueue(responseQueue); if (DEBUG) { DEBUGGER.debug("Destination: {}", destination); } consumer = session.createConsumer(destination, "JMSCorrelationID='" + messageId + "'"); } if (DEBUG) { DEBUGGER.debug("MessageConsumer: {}", consumer); } ObjectMessage message = (ObjectMessage) consumer.receive(timeout); if (DEBUG) { DEBUGGER.debug("ObjectMessage: {}", message); } if (message == null) { throw new UtilityException("Failed to retrieve message within the timeout specified."); } response = message.getObject(); message.acknowledge(); if (DEBUG) { DEBUGGER.debug("Object: {}", response); } } catch (JMSException jx) { throw new UtilityException(jx.getMessage(), jx); } finally { try { // Clean up if (!(session == null)) { session.close(); } if (!(conn == null)) { conn.close(); conn.stop(); } } catch (JMSException jx) { ERROR_RECORDER.error(jx.getMessage(), jx); } } return response; }
From source file:pl.edu.amu.lasuch.model.ProductConverter.java
@Override public Object fromMessage(Message msg) throws JMSException, MessageConversionException { ObjectMessage message = (ObjectMessage) msg; return message.getObject(); }
From source file:nl.han.dare2date.service.AbonneeNotification.java
public void onMessage(Message message) { if (message instanceof ObjectMessage) { ObjectMessage msg = (ObjectMessage) message; try {/*from ww w .ja v a 2 s. c o m*/ if (msg.getObject() instanceof Registration) { Registration r = (Registration) msg.getObject(); System.out.println(r.getUser().getEmail()); } } catch (JMSException e) { logger.warn("Get object from message", e); } } }
From source file:nl.han.dare2date.service.UserNotification.java
public void onMessage(Message message) { if (message instanceof ObjectMessage) { ObjectMessage msg = (ObjectMessage) message; try {/* ww w. jav a 2 s . c o m*/ if (msg.getObject() instanceof Registration) { Registration r = (Registration) msg.getObject(); sendEmail(r.getUser()); System.out.println(r.getUser().getLastname()); } } catch (JMSException e) { logger.warn("Get object from message", e); } } }
From source file:org.chtijbug.drools.platform.backend.jms.JMSHistoryEventListener.java
@Transactional public void onMessage(Message message) { HistoryEvent historyEvent = null;/* ww w . j a va2 s . c om*/ try { if (message instanceof ObjectMessage) { ObjectMessage objectMessage = (ObjectMessage) message; Object messageContent = objectMessage.getObject(); historyEvent = (HistoryEvent) messageContent; try { AbstractEventHandlerStrategy strategy = messageHandlerResolver .resolveMessageHandler(historyEvent); strategy.handleMessage(historyEvent); } catch (Throwable e) { throw Throwables.propagate(e); } ObjectMessage msg = (ObjectMessage) message; LOG.debug("Consumed message: " + msg.toString()); } } catch (Exception e) { LOG.error("Consumed message: " + e.toString() + " message content " + message.toString()); LOG.error(" message " + message.toString()); LOG.error(" object content " + historyEvent.toString()); } }
From source file:dk.netarkivet.common.distribute.JMSConnection.java
/** * Unwraps a NetarkivetMessage from an ObjectMessage. * * @param msg a javax.jms.ObjectMessage// ww w . j a v a 2s .c o m * * @return a NetarkivetMessage * * @throws ArgumentNotValid when msg in valid or format of JMS Object * message is invalid */ public static NetarkivetMessage unpack(Message msg) throws ArgumentNotValid { ArgumentNotValid.checkNotNull(msg, "msg"); ObjectMessage objMsg; try { objMsg = (ObjectMessage) msg; } catch (ClassCastException e) { log.warn("Invalid message type: " + msg.getClass()); throw new ArgumentNotValid("Invalid message type: " + msg.getClass()); } NetarkivetMessage netMsg; String classname = "Unknown class"; // for error reporting purposes try { classname = objMsg.getObject().getClass().getName(); netMsg = (NetarkivetMessage) objMsg.getObject(); // Note: Id is only updated if the message does not already have an // id. On unpack, this means the first time the message is received. // FIXME Fix for NAS-2043 doesn't seem to work //String randomID = UUID.randomUUID().toString(); //netMsg.updateId(randomID); netMsg.updateId(msg.getJMSMessageID()); } catch (ClassCastException e) { log.warn("Invalid message type: " + classname, e); throw new ArgumentNotValid("Invalid message type: " + classname, e); } catch (Exception e) { String message = "Message invalid. Unable to unpack " + "message: " + classname; log.warn(message, e); throw new ArgumentNotValid(message, e); } log.trace("Unpacked message '" + netMsg + "'"); return netMsg; }