Example usage for javax.jms JMSException JMSException

List of usage examples for javax.jms JMSException JMSException

Introduction

In this page you can find the example usage for javax.jms JMSException JMSException.

Prototype

public JMSException(String reason) 

Source Link

Document

Constructs a JMSException with the specified reason and with the error code defaulting to null.

Usage

From source file:org.apache.rocketmq.jms.RocketMQConnection.java

@Override
public ConnectionConsumer createSharedDurableConnectionConsumer(Topic topic, String subscriptionName,
        String messageSelector, ServerSessionPool sessionPool, int maxMessages) throws JMSException {
    //todo/*from   ww w . j a  va2 s.co m*/
    throw new JMSException("Not support yet");
}

From source file:org.apache.rocketmq.jms.RocketMQConnection.java

@Override
public ExceptionListener getExceptionListener() throws JMSException {
    //todo/*from   w ww  .ja va  2s. co m*/
    throw new JMSException("Not support yet");
}

From source file:org.apache.rocketmq.jms.RocketMQConnection.java

@Override
public void setExceptionListener(ExceptionListener listener) throws JMSException {
    //todo// ww  w . j  ava  2 s  . co m
    throw new JMSException("Not support yet");
}

From source file:org.apache.rocketmq.jms.RocketMQProducer.java

private void sendSync(com.alibaba.rocketmq.common.message.Message rmqMsg) throws JMSException {
    SendResult sendResult;//  w w w.j a va 2 s .c o m

    try {
        sendResult = mqProducer.send(rmqMsg);
    } catch (Exception e) {
        throw new JMSException(format("Fail to send message. Error: %s", getStackTrace(e)));
    }

    if (sendResult != null && sendResult.getSendStatus() == SendStatus.SEND_OK) {
        log.debug("Success to send message[key={}]", rmqMsg.getKeys());
        return;
    } else {
        throw new JMSException(
                format("Sending message error with result status:%s", sendResult.getSendStatus().name()));
    }
}

From source file:org.apache.rocketmq.jms.RocketMQProducer.java

private void sendAsync(com.alibaba.rocketmq.common.message.Message rmqMsg,
        CompletionListener completionListener) throws JMSException {
    try {/*from  w ww.j ava 2 s  . c o  m*/
        mqProducer.send(rmqMsg, new SendCompletionListener(completionListener));
    } catch (Exception e) {
        throw new JMSException(format("Fail to send message. Error: %s", getStackTrace(e)));
    }
}

From source file:org.apache.rocketmq.jms.RocketMQProducer.java

private com.alibaba.rocketmq.common.message.Message createRmqMessage(Message message, String topicName)
        throws JMSException {
    RocketMQMessage jmsMsg = (RocketMQMessage) message;
    initJMSHeaders(jmsMsg, destination);
    com.alibaba.rocketmq.common.message.Message rmqMsg = null;
    try {//from w w w .j  a  v a2  s .c o m
        rmqMsg = MessageConverter.convert2RMQMessage(jmsMsg);
    } catch (Exception e) {
        throw new JMSException(format("Fail to convert to RocketMQ message. Error: %s", getStackTrace(e)));
    }

    return rmqMsg;
}

From source file:org.apache.rocketmq.jms.support.MessageConverter.java

public static JmsContent getContentFromJms(javax.jms.Message jmsMessage) throws JMSException {
    if (jmsMessage == null) {
        return null;
    }//from   w  w  w  . j  a va  2 s .  com

    JmsContent jmsContent = new JmsContent();
    if (jmsMessage instanceof TextMessage) {
        if (StringUtils.isEmpty(((TextMessage) jmsMessage).getText())) {
            throw new IllegalArgumentException("Message body length is zero");
        }
        jmsContent.setMessageModel(MSGMODEL_TEXT);
        jmsContent.setContent(string2Bytes(((TextMessage) jmsMessage).getText(), Charsets.UTF_8.toString()));
    } else if (jmsMessage instanceof ObjectMessage) {
        if (((ObjectMessage) jmsMessage).getObject() == null) {
            throw new IllegalArgumentException("Message body length is zero");
        }
        try {
            jmsContent.setMessageModel(MSGMODEL_OBJ);
            jmsContent.setContent(objectSerialize(((ObjectMessage) jmsMessage).getObject()));
        } catch (IOException e) {
            throw new JMSException(e.getMessage());
        }
    } else if (jmsMessage instanceof BytesMessage) {
        RocketMQBytesMessage bytesMessage = (RocketMQBytesMessage) jmsMessage;
        if (bytesMessage.getBodyLength() == 0) {
            throw new IllegalArgumentException("Message body length is zero");
        }
        jmsContent.setMessageModel(MSGMODEL_BYTES);
        jmsContent.setContent(bytesMessage.getData());
    } else {
        throw new IllegalArgumentException("Unknown message type " + jmsMessage.getJMSType());
    }

    return jmsContent;
}

From source file:org.apache.rocketmq.jms.support.MessageConverter.java

public static RocketMQMessage convert2JMSMessage(MessageExt msg) throws JMSException {
    if (msg == null) {
        return null;
    }//  w w  w . j ava 2s .c om

    RocketMQMessage message;
    if (MSGMODEL_BYTES.equals(msg.getUserProperty(JMS_MSGMODEL))) {
        message = new RocketMQBytesMessage(msg.getBody());
    } else if (MSGMODEL_OBJ.equals(msg.getUserProperty(JMS_MSGMODEL))) {
        try {
            message = new RocketMQObjectMessage(objectDeserialize(msg.getBody()));
        } catch (Exception e) {
            throw new JMSException(e.getMessage());
        }
    } else if (MSGMODEL_TEXT.equals(msg.getUserProperty(JMS_MSGMODEL))) {
        message = new RocketMQTextMessage(bytes2String(msg.getBody(), Charsets.UTF_8.toString()));
    } else {
        // rocketmq producer sends bytesMessage without setting JMS_MSGMODEL.
        message = new RocketMQBytesMessage(msg.getBody());
    }

    //-------------------------set headers-------------------------
    Map<String, Object> properties = new HashMap<String, Object>();

    message.setHeader(Constant.JMS_MESSAGE_ID, "ID:" + msg.getMsgId());

    if (msg.getReconsumeTimes() > 0) {
        message.setHeader(Constant.JMS_REDELIVERED, Boolean.TRUE);
    } else {
        message.setHeader(Constant.JMS_REDELIVERED, Boolean.FALSE);
    }

    Map<String, String> propertiesMap = msg.getProperties();
    if (propertiesMap != null) {
        for (String properName : propertiesMap.keySet()) {
            String properValue = propertiesMap.get(properName);
            if (Constant.JMS_DESTINATION.equals(properName)) {
                String destinationStr = properValue;
                if (null != destinationStr) {
                    List<String> msgTuple = Arrays.asList(destinationStr.split(":"));
                    //todo: what about Queue?
                    RocketMQTopic topic = null;
                    if (msgTuple.size() == 1) {
                        topic = new RocketMQTopic(msgTuple.get(0));
                    } else {
                        topic = new RocketMQTopic(msgTuple.get(0), msgTuple.get(1));
                    }
                    message.setHeader(Constant.JMS_DESTINATION, topic);
                }
            } else if (Constant.JMS_DELIVERY_MODE.equals(properName)
                    || Constant.JMS_PRIORITY.equals(properName)) {
                message.setHeader(properName, properValue);
            } else if (Constant.JMS_TIMESTAMP.equals(properName)
                    || Constant.JMS_EXPIRATION.equals(properName)) {
                message.setHeader(properName, properValue);
            } else if (Constant.JMS_CORRELATION_ID.equals(properName) || Constant.JMS_TYPE.equals(properName)) {
                message.setHeader(properName, properValue);
            } else if (Constant.JMS_MESSAGE_ID.equals(properName)
                    || Constant.JMS_REDELIVERED.equals(properName)) {
                //JMS_MESSAGE_ID should set by msg.getMsgID()
                continue;
            } else {
                properties.put(properName, properValue);
            }
        }
    }

    //Handle System properties, put into header.
    message.setProperties(properties);

    return message;
}

From source file:org.apache.synapse.transport.jms.JMSSender.java

/**
 * Create a JMS Message from the given MessageContext and using the given
 * session//from  w  w  w .  j  av a  2s. com
 *
 * @param msgContext the MessageContext
 * @param session    the JMS session
 * @return a JMS message from the context and session
 * @throws JMSException on exception
 * @throws AxisFault on exception
 */
private Message createJMSMessage(MessageContext msgContext, Session session) throws JMSException, AxisFault {

    Message message = null;
    String msgType = getProperty(msgContext, JMSConstants.JMS_MESSAGE_TYPE);

    // check the first element of the SOAP body, do we have content wrapped using the
    // default wrapper elements for binary (BaseConstants.DEFAULT_BINARY_WRAPPER) or
    // text (BaseConstants.DEFAULT_TEXT_WRAPPER) ? If so, do not create SOAP messages
    // for JMS but just get the payload in its native format
    String jmsPayloadType = guessMessageType(msgContext);

    if (jmsPayloadType == null) {

        OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext);
        MessageFormatter messageFormatter = null;
        try {
            messageFormatter = TransportUtils.getMessageFormatter(msgContext);
        } catch (AxisFault axisFault) {
            throw new JMSException("Unable to get the message formatter to use");
        }

        String contentType = messageFormatter.getContentType(msgContext, format, msgContext.getSoapAction());

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            messageFormatter.writeTo(msgContext, format, baos, true);
            baos.flush();
        } catch (IOException e) {
            handleException("IO Error while creating BytesMessage", e);
        }

        if (msgType != null && JMSConstants.JMS_BYTE_MESSAGE.equals(msgType)
                || contentType.indexOf(HTTPConstants.HEADER_ACCEPT_MULTIPART_RELATED) > -1) {
            message = session.createBytesMessage();
            BytesMessage bytesMsg = (BytesMessage) message;
            bytesMsg.writeBytes(baos.toByteArray());
        } else {
            message = session.createTextMessage(); // default
            TextMessage txtMsg = (TextMessage) message;
            txtMsg.setText(new String(baos.toByteArray()));
        }
        message.setStringProperty(BaseConstants.CONTENT_TYPE, contentType);

    } else if (JMSConstants.JMS_BYTE_MESSAGE.equals(jmsPayloadType)) {
        message = session.createBytesMessage();
        BytesMessage bytesMsg = (BytesMessage) message;
        OMElement wrapper = msgContext.getEnvelope().getBody()
                .getFirstChildWithName(BaseConstants.DEFAULT_BINARY_WRAPPER);
        OMNode omNode = wrapper.getFirstOMChild();
        if (omNode != null && omNode instanceof OMText) {
            Object dh = ((OMText) omNode).getDataHandler();
            if (dh != null && dh instanceof DataHandler) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try {
                    ((DataHandler) dh).writeTo(baos);
                } catch (IOException e) {
                    handleException("Error serializing binary content of element : "
                            + BaseConstants.DEFAULT_BINARY_WRAPPER, e);
                }
                bytesMsg.writeBytes(baos.toByteArray());
            }
        }

    } else if (JMSConstants.JMS_TEXT_MESSAGE.equals(jmsPayloadType)) {
        message = session.createTextMessage();
        TextMessage txtMsg = (TextMessage) message;
        txtMsg.setText(msgContext.getEnvelope().getBody()
                .getFirstChildWithName(BaseConstants.DEFAULT_TEXT_WRAPPER).getText());
    }

    // set the JMS correlation ID if specified
    String correlationId = getProperty(msgContext, JMSConstants.JMS_COORELATION_ID);
    if (correlationId == null && msgContext.getRelatesTo() != null) {
        correlationId = msgContext.getRelatesTo().getValue();
    }

    if (correlationId != null) {
        message.setJMSCorrelationID(correlationId);
    }

    if (msgContext.isServerSide()) {
        // set SOAP Action as a property on the JMS message
        setProperty(message, msgContext, BaseConstants.SOAPACTION);
    } else {
        String action = msgContext.getOptions().getAction();
        if (action != null) {
            message.setStringProperty(BaseConstants.SOAPACTION, action);
        }
    }

    JMSUtils.setTransportHeaders(msgContext, message);
    return message;
}

From source file:org.dhatim.routing.jms.activemq.ActiveMQProvider.java

private void createQueueSession() throws JMSException {
    try {/*  w w w. ja v a2  s. com*/
        ConnectionFactory connectionFactory = getConnectionFactory();

        queueConnection = ((QueueConnectionFactory) connectionFactory).createQueueConnection();
        queueSession = ((QueueConnection) queueConnection).createQueueSession(false,
                QueueSession.AUTO_ACKNOWLEDGE);

        // Start the connection...
        queueConnection.start();
    } catch (JMSException e) {
        close(queueConnection, queueSession);
        throw e;
    } catch (Throwable t) {
        close(queueConnection, queueSession);
        throw (JMSException) (new JMSException("Unexpected exception while creating JMS Session.")
                .initCause(t));
    }
}