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.logicblaze.lingo.jms.impl.MultiplexingRequestor.java

protected JMSException createJMSException(Exception e) {
    JMSException answer = new JMSException(e.toString());
    answer.setLinkedException(e);
    return answer;
}

From source file:org.mule.transport.jms.Jms11Support.java

public MessageConsumer createConsumer(Session session, Destination destination, String messageSelector,
        boolean noLocal, String durableName, boolean topic, ImmutableEndpoint endpoint) throws JMSException {
    if (durableName == null) {
        if (topic) {
            return session.createConsumer(destination, messageSelector, noLocal);
        } else {//from www.  j  a  va  2s. c  o m
            return session.createConsumer(destination, messageSelector);
        }
    } else {
        if (topic) {
            return session.createDurableSubscriber((Topic) destination, durableName, messageSelector, noLocal);
        } else {
            throw new JMSException("A durable subscriber name was set but the destination was not a Topic");
        }
    }
}

From source file:org.mule.transport.jms.Jms11Support.java

public Destination createDestination(Session session, String name, boolean topic, ImmutableEndpoint endpoint)
        throws JMSException {
    if (connector.isJndiDestinations()) {
        try {/*www.  ja va2 s.co  m*/
            Destination dest = getJndiDestination(name);
            if (dest != null) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            MessageFormat.format("Destination {0} located in JNDI, will use it now", name));
                }
                return dest;
            } else {
                throw new JMSException("JNDI destination not found with name: " + name);
            }
        } catch (JMSException e) {
            if (connector.isForceJndiDestinations()) {
                throw e;
            } else {
                logger.warn("Unable to look up JNDI destination " + name + ": " + e.getMessage());
            }
        }
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Using non-JNDI destination " + name + ", will create one now");
    }

    if (session == null) {
        throw new IllegalArgumentException("Session cannot be null when creating a destination");
    }
    if (name == null) {
        throw new IllegalArgumentException("Destination name cannot be null when creating a destination");
    }

    if (topic) {
        return session.createTopic(name);
    } else {
        return session.createQueue(name);
    }
}

From source file:org.mule.transport.jms.Jms11Support.java

protected Destination getJndiDestination(String name) throws JMSException {
    Object temp;//from w w w.  j  a v a 2  s  .  c  o m
    try {
        if (logger.isDebugEnabled()) {
            logger.debug(MessageFormat.format("Looking up {0} from JNDI", name));
        }
        temp = connector.lookupFromJndi(name);
    } catch (NamingException e) {
        if (logger.isDebugEnabled()) {
            logger.debug(e);
        }
        String message = MessageFormat.format("Failed to look up destination {0}. Reason: {1}", name,
                e.getMessage());
        throw new JMSException(message);
    }

    if (temp != null) {
        if (temp instanceof Destination) {
            return (Destination) temp;
        }
    }
    return null;
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

public static Message toMessage(Object object, Session session) throws JMSException {
    if (object instanceof Message) {
        return (Message) object;
    } else if (object instanceof String) {
        return stringToMessage((String) object, session);
    } else if (object instanceof Map<?, ?> && validateMapMessageType((Map<?, ?>) object)) {
        return mapToMessage((Map<?, ?>) object, session);
    } else if (object instanceof InputStream) {
        return inputStreamToMessage((InputStream) object, session);
    } else if (object instanceof List<?>) {
        return listToMessage((List<?>) object, session);
    } else if (object instanceof byte[]) {
        return byteArrayToMessage((byte[]) object, session);
    } else if (object instanceof Serializable) {
        return serializableToMessage((Serializable) object, session);
    } else if (object instanceof OutputHandler) {
        return outputHandlerToMessage((OutputHandler) object, session);
    } else {// w w  w . ja v a  2 s  .  c  o m
        throw new JMSException(
                "Source was not of a supported type. Valid types are Message, String, Map, InputStream, List, byte[], Serializable or OutputHandler, "
                        + "but was " + ClassUtils.getShortClassName(object, "<null>"));
    }
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

private static Message inputStreamToMessage(InputStream value, Session session) throws JMSException {
    StreamMessage streamMessage = session.createStreamMessage();
    byte[] buffer = new byte[4096];
    int len;//  ww  w  .j  a v a2  s  .c  o  m

    try {
        while ((len = value.read(buffer)) != -1) {
            streamMessage.writeBytes(buffer, 0, len);
        }
    } catch (IOException e) {
        throw new JMSException("Failed to read input stream to create a stream message: " + e);
    } finally {
        IOUtils.closeQuietly(value);
    }

    return streamMessage;
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

private static Message outputHandlerToMessage(OutputHandler value, Session session) throws JMSException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {// w ww.jav a  2s. co m
        value.write(null, output);
    } catch (IOException e) {
        JMSException j = new JMSException("Could not serialize OutputHandler.");
        j.initCause(e);
        throw j;
    }

    BytesMessage bMsg = session.createBytesMessage();
    bMsg.writeBytes(output.toByteArray());

    return bMsg;
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

public static Object toObject(Message source, String jmsSpec, String encoding)
        throws JMSException, IOException {
    if (source instanceof ObjectMessage) {
        return ((ObjectMessage) source).getObject();
    } else if (source instanceof MapMessage) {
        Map<String, Object> map = new HashMap<String, Object>();
        MapMessage m = (MapMessage) source;

        for (Enumeration<?> e = m.getMapNames(); e.hasMoreElements();) {
            String name = (String) e.nextElement();
            Object obj = m.getObject(name);
            map.put(name, obj);//from w  w  w .  ja v a 2 s  .  c  o  m
        }

        return map;
    } else if (source instanceof TextMessage) {
        return ((TextMessage) source).getText();
    } else if (source instanceof BytesMessage) {
        return toByteArray(source, jmsSpec, encoding);
    } else if (source instanceof StreamMessage) {
        List<Object> result = new ArrayList<Object>();
        try {
            StreamMessage sMsg = (StreamMessage) source;
            Object obj;
            while ((obj = sMsg.readObject()) != null) {
                result.add(obj);
            }
        } catch (MessageEOFException eof) {
            // ignored
        } catch (Exception e) {
            throw new JMSException("Failed to extract information from JMS Stream Message: " + e);
        }
        return result;
    }

    // what else is there to do?
    return source;
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

/**
 * @param message the message to receive the bytes from. Note this only works for
 *                TextMessge, ObjectMessage, StreamMessage and BytesMessage.
 * @param jmsSpec indicates the JMS API version, either
 *                {@link JmsConstants#JMS_SPECIFICATION_102B} or
 *                {@link JmsConstants#JMS_SPECIFICATION_11}. Any other value
 *                including <code>null</code> is treated as fallback to
 *                {@link JmsConstants#JMS_SPECIFICATION_102B}.
 * @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 failure occurs while reading the stream and
 *                             converting the message data
 *///from ww  w  . ja v a 2 s  . c  o m
public static byte[] toByteArray(Message message, String jmsSpec, String encoding)
        throws JMSException, IOException {
    if (message instanceof BytesMessage) {
        BytesMessage bMsg = (BytesMessage) message;
        bMsg.reset();

        if (JmsConstants.JMS_SPECIFICATION_11.equals(jmsSpec)) {
            long bmBodyLength = bMsg.getBodyLength();
            if (bmBodyLength > Integer.MAX_VALUE) {
                throw new JMSException("Size of BytesMessage exceeds Integer.MAX_VALUE; "
                        + "please consider using JMS StreamMessage instead");
            }

            if (bmBodyLength > 0) {
                byte[] bytes = new byte[(int) bmBodyLength];
                bMsg.readBytes(bytes);
                return bytes;
            } else {
                return ArrayUtils.EMPTY_BYTE_ARRAY;
            }
        } else {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
            byte[] buffer = new byte[4096];
            int len;

            while ((len = bMsg.readBytes(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }

            if (baos.size() > 0) {
                return baos.toByteArray();
            } else {
                return ArrayUtils.EMPTY_BYTE_ARRAY;
            }
        }
    } else if (message instanceof StreamMessage) {
        StreamMessage sMsg = (StreamMessage) message;
        sMsg.reset();

        ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
        byte[] buffer = new byte[4096];
        int len;

        while ((len = sMsg.readBytes(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }

        return baos.toByteArray();
    } else if (message instanceof ObjectMessage) {
        ObjectMessage oMsg = (ObjectMessage) message;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(baos);
        os.writeObject(oMsg.getObject());
        os.flush();
        os.close();
        return baos.toByteArray();
    } else if (message instanceof TextMessage) {
        TextMessage tMsg = (TextMessage) message;
        String tMsgText = tMsg.getText();

        if (null == tMsgText) {
            // Avoid creating new instances of byte arrays, even empty ones. The
            // load on this part of the code can be high.
            return ArrayUtils.EMPTY_BYTE_ARRAY;
        } else {
            return tMsgText.getBytes(encoding);
        }
    } else {
        throw new JMSException("Cannot get bytes from Map Message");
    }
}

From source file:org.nuxeo.ecm.core.jms.CoreEventPublisher.java

/**
 * Retrieves a new JMS Connection from the pool.
 *
 * @return a <code>QueueConnection</code>
 * @throws JMSException if the connection could not be retrieved
 *//*ww  w . j  av  a  2 s. c  om*/
private TopicConnection getTopicConnection() throws JMSException {
    try {
        return getTopicConnectionFactory().createTopicConnection();
    } catch (NamingException e) {
        log.error("Failed too lookup topic connection factory", e);
        throw new JMSException("Failed to lookup topic connection factory: " + e.getMessage());
    }
}