List of usage examples for javax.jms TextMessage propertyExists
boolean propertyExists(String name) throws JMSException;
From source file:org.openanzo.combus.CombusConnection.java
/** * Send a request to a destination and wait for a response * /*from w w w. j ava 2s.com*/ * @param context * context for this operational call * @param destinationName * destination queue for this request * @param request * request message * @param timeout * timeout for waiting for a response * @return response message * @throws AnzoException * if there was an exception sending request, or a timeout waiting for a response */ public TextMessage requestResponse(IOperationContext context, String destinationName, Message request, long timeout) throws AnzoException { Destination destination = null; if (context.getAttribute(OPTIONS.DATASOURCE) != null) { URI datasourceUri = (URI) context.getAttribute(OPTIONS.DATASOURCE); Queue defaultDestination = (Queue) destinations.get(destinationName); if (datasourceUri.toString().equals("http://openanzo.org/datasource/systemDatasource")) { destination = defaultDestination; } else { String queueNamePrefix = UriGenerator.generateEncapsulatedString("", datasourceUri.toString()) + "/"; try { String[] parts = StringUtils.split(defaultDestination.getQueueName(), '/'); String queue = "services/" + queueNamePrefix + parts[1]; destination = destinations.get(queue); if (destination == null) { destination = session.createQueue(queue); destinations.put(queue, destination); } } catch (JMSException e) { throw new AnzoException(ExceptionConstants.COMBUS.NO_SUCH_DESTINATION, destinationName); } } } else { destination = destinations.get(destinationName); if (destination == null) { throw new AnzoException(ExceptionConstants.COMBUS.NO_SUCH_DESTINATION, destinationName); } } if (context.getAttribute(COMBUS.TIMEOUT) != null) { timeout = (Long) context.getAttribute(COMBUS.TIMEOUT); } String correlationId = context.getOperationId(); if (correlationId == null) { correlationId = UUID.randomUUID().toString(); } try { request.setJMSCorrelationID(correlationId); request.setJMSReplyTo(tempQueue); request.setIntProperty(SerializationConstants.protocolVersion, Constants.VERSION); if (context.getOperationPrincipal() != null && !context.getOperationPrincipal().getName().equals(this.userName)) { request.setStringProperty(SerializationConstants.runAsUser, context.getOperationPrincipal().getName()); } if (context.getAttribute(OPTIONS.PRIORITY) != null) { Integer priority = (Integer) context.getAttribute(OPTIONS.PRIORITY); request.setJMSPriority(priority); messageProducer.setPriority(priority); } else { messageProducer.setPriority(4); } if (context.getAttribute(OPTIONS.SKIPCACHE) != null) { request.setBooleanProperty(OPTIONS.SKIPCACHE, context.getAttribute(OPTIONS.SKIPCACHE, Boolean.class)); } if (log.isTraceEnabled()) { log.trace(LogUtils.COMBUS_MARKER, MessageUtils.prettyPrint(request, "Sending Request: (destination=" + destination + ")")); } messageProducer.send(destination, request); } catch (JMSException jmsex) { performDisconnect(true); throw new AnzoException(ExceptionConstants.COMBUS.COULD_NOT_PUBLISH, jmsex); } lock.lock(); try { Collection<TextMessage> messageSet = correlationIdToMessage.remove(correlationId); long start = System.currentTimeMillis(); while (messageSet == null) { if (timeout <= 0) { try { newMessage.await(2, TimeUnit.SECONDS); } catch (InterruptedException ie) { throw new AnzoException(ExceptionConstants.COMBUS.INTERRUPTED, correlationId); } if (closed || closing) { throw new AnzoException(ExceptionConstants.COMBUS.INTERRUPTED, correlationId); } messageSet = correlationIdToMessage.remove(correlationId); } else { try { newMessage.await(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException ie) { throw new AnzoException(ExceptionConstants.COMBUS.INTERRUPTED, correlationId); } if (closed || closing) { throw new AnzoException(ExceptionConstants.COMBUS.INTERRUPTED, correlationId); } messageSet = correlationIdToMessage.remove(correlationId); if (!connected) { log.error(LogUtils.COMBUS_MARKER, "Request Response failed because connection was closed"); throw new AnzoException(ExceptionConstants.COMBUS.JMS_NOT_CONNECTED, correlationId); } if (messageSet == null && ((timeout > 0) && ((System.currentTimeMillis() - start) > timeout))) { throw new AnzoException(ExceptionConstants.COMBUS.NO_SERVER_RESPONSE, correlationId); } } } try { TextMessage message = messageSet.iterator().next(); if (log.isTraceEnabled()) { log.trace(LogUtils.COMBUS_MARKER, MessageUtils.prettyPrint(message, "Received Response:")); } if (message.getBooleanProperty(SerializationConstants.operationFailed)) { long errorCodes = message.propertyExists(SerializationConstants.errorTags) ? message.getLongProperty(SerializationConstants.errorCode) : ExceptionConstants.COMBUS.JMS_SERVICE_EXCEPTION; // if available, use enumerated args, since these can be reconstruct an AnzoException correctly. List<String> args = new ArrayList<String>(); for (int i = 0; true; i++) { String errorArg = message.getStringProperty(SerializationConstants.errorMessageArg + i); if (errorArg == null) { break; } args.add(errorArg); } // NOTE: This doesn't really make any sense, but it was here before and it's better to be too verbose than not verbose enough // when it comes to error messages, so it stays. For now at least. -jpbetz if (args.isEmpty()) { args.add(message.getText()); } throw new AnzoException(errorCodes, args.toArray(new String[0])); } /*Object prp = context.getAttribute("populateResponseProperties"); if (prp != null && ((Boolean) prp)) { HashMap<String, Object> props = new HashMap<String, Object>(); Enumeration<String> keys = message.getPropertyNames(); while (keys.hasMoreElements()) { String key = keys.nextElement(); props.put(key, message.getObjectProperty(key)); } context.setAttribute("responseProperties", props); }*/ return message; } catch (JMSException jmsex) { log.debug(LogUtils.COMBUS_MARKER, Messages.formatString( ExceptionConstants.COMBUS.ERROR_PROCESSING_MESSGE, "request response"), jmsex); } return null; } finally { lock.unlock(); } }
From source file:org.openanzo.combus.CombusConnection.java
/** * Send a request to a destination and wait for a response * //from w w w.j a v a 2 s.c o m * @param context * context for this operational call * @param destinationName * destination queue for this request * @param request * request message * @param timeout * timeout for waiting for a response * @param messageHandler * the handler of multiple messages * @throws AnzoException * if there was an exception sending request, or a timeout waiting for a response */ public void requestMultipleResponse(IOperationContext context, String destinationName, Message request, long timeout, IMessageHandler messageHandler) throws AnzoException { Destination destination = null; if (context.getAttribute(OPTIONS.DATASOURCE) != null) { URI datasourceUri = (URI) context.getAttribute(OPTIONS.DATASOURCE); Queue defaultDestination = (Queue) destinations.get(destinationName); if (datasourceUri.toString().equals("http://openanzo.org/datasource/systemDatasource")) { destination = defaultDestination; } else { String queueNamePrefix = UriGenerator.generateEncapsulatedString("", datasourceUri.toString()) + "/"; try { String[] parts = StringUtils.split(defaultDestination.getQueueName(), '/'); String queue = "services/" + queueNamePrefix + parts[1]; destination = destinations.get(queue); if (destination == null) { destination = session.createQueue(queue); destinations.put(queue, destination); } } catch (JMSException e) { throw new AnzoException(ExceptionConstants.COMBUS.NO_SUCH_DESTINATION, destinationName); } } } else { destination = destinations.get(destinationName); if (destination == null) { throw new AnzoException(ExceptionConstants.COMBUS.NO_SUCH_DESTINATION, destinationName); } } String correlationId = context.getOperationId(); if (correlationId == null) { correlationId = UUID.randomUUID().toString(); } try { request.setJMSCorrelationID(correlationId); request.setJMSReplyTo(tempQueue); request.setIntProperty(SerializationConstants.protocolVersion, Constants.VERSION); if (context.getOperationPrincipal() != null && !context.getOperationPrincipal().getName().equals(this.userName)) { request.setStringProperty(SerializationConstants.runAsUser, context.getOperationPrincipal().getName()); } if (context.getAttribute(OPTIONS.PRIORITY) != null) { Integer priority = (Integer) context.getAttribute(OPTIONS.PRIORITY); request.setJMSPriority(priority); messageProducer.setPriority(priority); } else { messageProducer.setPriority(4); } if (context.getAttribute(OPTIONS.SKIPCACHE) != null) { request.setBooleanProperty(OPTIONS.SKIPCACHE, context.getAttribute(OPTIONS.SKIPCACHE, Boolean.class)); } if (context.getAttribute(OPTIONS.INCLUDEMETADATAGRAPHS) != null) { request.setBooleanProperty(OPTIONS.INCLUDEMETADATAGRAPHS, context.getAttribute(OPTIONS.INCLUDEMETADATAGRAPHS, Boolean.class)); } if (log.isTraceEnabled()) { log.trace(LogUtils.COMBUS_MARKER, MessageUtils.prettyPrint(request, "Sending Request: (destination=" + destination + ")")); } messageProducer.send(destination, request); } catch (JMSException jmsex) { performDisconnect(true); throw new AnzoException(ExceptionConstants.COMBUS.COULD_NOT_PUBLISH, jmsex); } lock.lock(); try { long start = System.currentTimeMillis(); boolean done = false; int seq = 0; while (!done) { Collection<TextMessage> messageSet = correlationIdToMessage.remove(correlationId); while (messageSet == null) { if (timeout <= 0) { try { newMessage.await(2, TimeUnit.SECONDS); } catch (InterruptedException ie) { throw new AnzoException(ExceptionConstants.COMBUS.INTERRUPTED, correlationId); } if (closed || closing) { throw new AnzoException(ExceptionConstants.COMBUS.INTERRUPTED, correlationId); } messageSet = correlationIdToMessage.remove(correlationId); } else { try { newMessage.await(timeout, TimeUnit.MILLISECONDS); } catch (InterruptedException ie) { throw new AnzoException(ExceptionConstants.COMBUS.INTERRUPTED, correlationId); } if (closed || closing) { throw new AnzoException(ExceptionConstants.COMBUS.INTERRUPTED, correlationId); } messageSet = correlationIdToMessage.remove(correlationId); if (!connected) { log.error(LogUtils.COMBUS_MARKER, Messages.formatString( ExceptionConstants.COMBUS.ERROR_PROCESSING_MESSGE, "connection closed")); throw new AnzoException(ExceptionConstants.COMBUS.JMS_NOT_CONNECTED, correlationId); } if (messageSet == null && ((timeout > -1) && ((System.currentTimeMillis() - start) > timeout))) { throw new AnzoException(ExceptionConstants.COMBUS.NO_SERVER_RESPONSE, correlationId); } } } try { for (TextMessage message : messageSet) { if (log.isTraceEnabled()) { log.trace(LogUtils.COMBUS_MARKER, MessageUtils.prettyPrint(message, "Recieved Response:")); } if (message.propertyExists("done")) { done = message.getBooleanProperty("done"); } else { done = true; } if (message.propertyExists("sequence")) { int sequence = message.getIntProperty("sequence"); if (sequence != seq) { throw new AnzoException(ExceptionConstants.COMBUS.NO_SERVER_RESPONSE, correlationId); } else { seq++; } } int totalSize = 0; if (message.propertyExists(SerializationConstants.totalSolutions)) { totalSize = message.getIntProperty(SerializationConstants.totalSolutions); } if (message.getBooleanProperty(SerializationConstants.operationFailed)) { long errorCodes = message.propertyExists(SerializationConstants.errorTags) ? message.getLongProperty(SerializationConstants.errorCode) : ExceptionConstants.COMBUS.JMS_SERVICE_EXCEPTION; // if available, use enumerated args, since these can be reconstruct an AnzoException correctly. List<String> args = new ArrayList<String>(); for (int i = 0; true; i++) { String errorArg = message .getStringProperty(SerializationConstants.errorMessageArg + i); if (errorArg == null) { break; } args.add(errorArg); } // NOTE: This doesn't really make any sense, but it was here before and it's better to be too verbose than not verbose enough // when it comes to error messages, so it stays. For now at least. -jpbetz if (args.isEmpty()) { args.add(message.getText()); } throw new AnzoException(errorCodes, args.toArray(new String[0])); } else { messageHandler.handleMessage(message, seq, done, totalSize); } } } catch (JMSException jmsex) { log.debug(LogUtils.COMBUS_MARKER, Messages.formatString( ExceptionConstants.COMBUS.ERROR_PROCESSING_MESSGE, "request multiple response"), jmsex); } } } finally { lock.unlock(); } }
From source file:org.wso2.carbon.appfactory.eventing.jms.AFMessageListener.java
@Override public void onMessage(Message message) { if (log.isDebugEnabled()) { if (message instanceof MapMessage) { try { String messageBody = ((MapMessage) message).getString(TopicPublisher.MESSAGE_BODY); log.debug("Received a message:" + messageBody); } catch (JMSException e) { log.error("Error while getting message content.", e); }//from ww w . ja v a2 s.c o m } } MapMessage mapMessage; if (message instanceof MapMessage) { mapMessage = (MapMessage) message; MessageStore.getInstance().addMessage(this.subscriptionId, mapMessage); } else if (message instanceof TextMessage) { //Todo:remove this. we only support mapMessages initially and below code is only for testing purpose. final TextMessage textMessage = (TextMessage) message; mapMessage = new MapMessage() { @Override public boolean getBoolean(String s) throws JMSException { return false; //To change body of implemented methods use File | Settings | File Templates. } @Override public byte getByte(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public short getShort(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public char getChar(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public int getInt(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public long getLong(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public float getFloat(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public double getDouble(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public String getString(String s) throws JMSException { return textMessage.getText(); } @Override public byte[] getBytes(String s) throws JMSException { return new byte[0]; //To change body of implemented methods use File | Settings | File Templates. } @Override public Object getObject(String s) throws JMSException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Enumeration getMapNames() throws JMSException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public void setBoolean(String s, boolean b) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setByte(String s, byte b) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setShort(String s, short i) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setChar(String s, char c) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setInt(String s, int i) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setLong(String s, long l) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setFloat(String s, float v) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setDouble(String s, double v) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setString(String s, String s2) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setBytes(String s, byte[] bytes) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setBytes(String s, byte[] bytes, int i, int i2) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setObject(String s, Object o) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public boolean itemExists(String s) throws JMSException { return false; //To change body of implemented methods use File | Settings | File Templates. } @Override public String getJMSMessageID() throws JMSException { return textMessage.getJMSMessageID(); } @Override public void setJMSMessageID(String s) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public long getJMSTimestamp() throws JMSException { return textMessage.getJMSTimestamp(); } @Override public void setJMSTimestamp(long l) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public byte[] getJMSCorrelationIDAsBytes() throws JMSException { return new byte[0]; //To change body of implemented methods use File | Settings | File Templates. } @Override public void setJMSCorrelationIDAsBytes(byte[] bytes) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setJMSCorrelationID(String s) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public String getJMSCorrelationID() throws JMSException { return textMessage.getJMSCorrelationID(); } @Override public Destination getJMSReplyTo() throws JMSException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public void setJMSReplyTo(Destination destination) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public Destination getJMSDestination() throws JMSException { return textMessage.getJMSDestination(); } @Override public void setJMSDestination(Destination destination) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public int getJMSDeliveryMode() throws JMSException { return textMessage.getJMSDeliveryMode(); } @Override public void setJMSDeliveryMode(int i) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public boolean getJMSRedelivered() throws JMSException { return textMessage.getJMSRedelivered(); } @Override public void setJMSRedelivered(boolean b) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public String getJMSType() throws JMSException { return textMessage.getJMSType(); } @Override public void setJMSType(String s) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public long getJMSExpiration() throws JMSException { return textMessage.getJMSExpiration(); } @Override public void setJMSExpiration(long l) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public int getJMSPriority() throws JMSException { return textMessage.getJMSPriority(); } @Override public void setJMSPriority(int i) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void clearProperties() throws JMSException { textMessage.clearProperties(); } @Override public boolean propertyExists(String s) throws JMSException { return textMessage.propertyExists(s); } @Override public boolean getBooleanProperty(String s) throws JMSException { return false; //To change body of implemented methods use File | Settings | File Templates. } @Override public byte getByteProperty(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public short getShortProperty(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public int getIntProperty(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public long getLongProperty(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public float getFloatProperty(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public double getDoubleProperty(String s) throws JMSException { return 0; //To change body of implemented methods use File | Settings | File Templates. } @Override public String getStringProperty(String s) throws JMSException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Object getObjectProperty(String s) throws JMSException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public Enumeration getPropertyNames() throws JMSException { return null; //To change body of implemented methods use File | Settings | File Templates. } @Override public void setBooleanProperty(String s, boolean b) throws JMSException { //To change body of implemented methods use File | Settings | File Templates. } @Override public void setByteProperty(String s, byte b) throws JMSException { textMessage.setByteProperty(s, b); } @Override public void setShortProperty(String s, short i) throws JMSException { textMessage.setShortProperty(s, i); } @Override public void setIntProperty(String s, int i) throws JMSException { textMessage.setIntProperty(s, i); } @Override public void setLongProperty(String s, long l) throws JMSException { textMessage.setLongProperty(s, l); } @Override public void setFloatProperty(String s, float v) throws JMSException { textMessage.setFloatProperty(s, v); } @Override public void setDoubleProperty(String s, double v) throws JMSException { textMessage.setDoubleProperty(s, v); } @Override public void setStringProperty(String s, String s2) throws JMSException { textMessage.setStringProperty(s, s2); } @Override public void setObjectProperty(String s, Object o) throws JMSException { textMessage.setObjectProperty(s, o); } @Override public void acknowledge() throws JMSException { textMessage.acknowledge(); } @Override public void clearBody() throws JMSException { textMessage.clearBody(); } }; MessageStore.getInstance().addMessage(this.subscriptionId, mapMessage); } }