Example usage for javax.jms Session createTextMessage

List of usage examples for javax.jms Session createTextMessage

Introduction

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

Prototype


TextMessage createTextMessage() throws JMSException;

Source Link

Document

Creates a TextMessage object.

Usage

From source file:com.adaptris.core.jms.MetadataCorrelationIdSourceTest.java

public void testAdaptrisMessageMetadataToJmsCorrelationId_EmptyValue() throws Exception {
    EmbeddedActiveMq broker = new EmbeddedActiveMq();
    JmsConnection conn = broker.getJmsConnection();
    try {//from w w w.j  a  va 2 s.c o m
        broker.start();
        start(conn);
        Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        AdaptrisMessage adpMsg = AdaptrisMessageFactory.getDefaultInstance().newMessage(TEXT);
        adpMsg.addMetadata(CORRELATIONID_KEY, "");
        TextMessage jmsMsg = session.createTextMessage();
        MetadataCorrelationIdSource mcs = new MetadataCorrelationIdSource(CORRELATIONID_KEY);
        mcs.processCorrelationId(adpMsg, jmsMsg);
        assertTrue(StringUtils.isEmpty(jmsMsg.getJMSCorrelationID()));
        session.close();
    } finally {
        stop(conn);
        broker.destroy();
    }
}

From source file:com.adaptris.core.jms.MetadataCorrelationIdSourceTest.java

public void testAdaptrisMessageMetadataToJmsCorrelationId_NoMetadataKey() throws Exception {
    EmbeddedActiveMq broker = new EmbeddedActiveMq();
    JmsConnection conn = broker.getJmsConnection();
    try {/*from  w ww. jav a2s . c om*/
        broker.start();
        start(conn);
        Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        AdaptrisMessage adpMsg = AdaptrisMessageFactory.getDefaultInstance().newMessage(TEXT);
        adpMsg.addMetadata(CORRELATIONID_KEY, TEXT2);
        TextMessage jmsMsg = session.createTextMessage();
        MetadataCorrelationIdSource mcs = new MetadataCorrelationIdSource();
        mcs.processCorrelationId(adpMsg, jmsMsg);
        assertNotSame(adpMsg.getMetadataValue(CORRELATIONID_KEY), jmsMsg.getJMSCorrelationID());
        session.close();
    } finally {
        stop(conn);
        broker.destroy();
    }
}

From source file:com.adaptris.core.jms.MetadataCorrelationIdSourceTest.java

public void testAdaptrisMessageMetadataToJmsCorrelationId() throws Exception {

    EmbeddedActiveMq broker = new EmbeddedActiveMq();
    JmsConnection conn = broker.getJmsConnection();
    try {// w w  w .  j ava2s  .c o m
        broker.start();
        start(conn);
        Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        AdaptrisMessage adpMsg = AdaptrisMessageFactory.getDefaultInstance().newMessage(TEXT);
        adpMsg.addMetadata(CORRELATIONID_KEY, TEXT2);
        TextMessage jmsMsg = session.createTextMessage();
        MetadataCorrelationIdSource mcs = new MetadataCorrelationIdSource(CORRELATIONID_KEY);
        mcs.processCorrelationId(adpMsg, jmsMsg);
        assertEquals(adpMsg.getMetadataValue(CORRELATIONID_KEY), jmsMsg.getJMSCorrelationID());
        session.close();
    } finally {
        stop(conn);
        broker.destroy();
    }
}

From source file:com.novaordis.gld.service.jms.activemq.CollocatedBrokerService.java

@Override
public void perform(Operation o) throws Exception {
    if (!isStarted()) {
        throw new IllegalStateException(this + " not started");
    }//from ww w.j a v  a2  s .  c o  m

    if (!(o instanceof Send)) {
        throw new IllegalArgumentException(o + " is not a Send operation");
    }

    Send send = (Send) o;
    final String payload = send.getPayload();

    if (trace) {
        log.trace(this + " performing " + send);
    }

    // figure what session to use based on the current policy in place
    String destinationName = send.getDestination().getName();

    jmsTemplate.send(destinationName, new MessageCreator() {
        @Override
        public Message createMessage(final Session session) throws JMSException {
            if (payload == null) {
                return session.createTextMessage();
            } else {
                return session.createTextMessage(payload);
            }

        }
    });
}

From source file:org.btc4j.jms.BtcDaemonCaller.java

public String sendReceive(final String destination, final String payload) {
    return jmsTemplate.execute(new SessionCallback<String>() {
        @Override//from   w w  w .j  a v  a 2  s .c  o m
        public String doInJms(Session session) throws JMSException {
            final TemporaryQueue replyQueue = session.createTemporaryQueue();
            jmsTemplate.send(destination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    TextMessage message = session.createTextMessage();
                    message.setJMSReplyTo(replyQueue);
                    message.setText(payload);
                    message.setStringProperty("btcapi:account", account);
                    message.setStringProperty("btcapi:password", password);
                    return message;
                }
            });
            return String.valueOf(jmsTemplate.receiveAndConvert(replyQueue));
        }
    });
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.jms.HermesJmsRequestTransport.java

private Message createTextMessage(SubmitContext submitContext, String requestContent, Session session)
        throws JMSException {
    TextMessage textMessageSend = session.createTextMessage();
    textMessageSend.setText(requestContent);
    return textMessageSend;
}

From source file:com.bleum.canton.jms.scheduler.AbstractJMSScheduler.java

/**
 * Send JMS message. Can be override.// ww  w. ja va2 s .  c  o  m
 * 
 * @param task
 */
protected void sendMessage(final JMSTask task) {
    if (jmsSender == null) {
        throw new RuntimeException("jmsSender is null.");
    }
    MessageCreator mc = new MessageCreator() {

        public Message createMessage(Session session) throws JMSException {
            TextMessage om = session.createTextMessage();
            om.setText(formMessage(task));
            om.setIntProperty("clientAck", clientAck);
            if (clientAck == JMSTaskConstant.CLIENT_ACKNOWLEDGE) {
                om.setLongProperty("taskId", task.getId());
                om.setJMSReplyTo(replyQueue);
            }
            return om;
        }
    };
    jmsSender.send(mc);
}

From source file:com.eviware.soapui.impl.wsdl.submit.transports.jms.HermesJmsRequestTransport.java

private Message createTextMessageFromAttachment(SubmitContext submitContext, Request request, Session session) {
    try {//from w  w  w.  j  a va  2 s .c  o  m
        String content = convertStreamToString(request.getAttachments()[0].getInputStream());
        TextMessage textMessageSend = session.createTextMessage();
        String messageBody = PropertyExpander.expandProperties(submitContext, content);
        textMessageSend.setText(messageBody);
        return textMessageSend;
    } catch (Exception e) {
        SoapUI.logError(e);
    }
    return null;
}

From source file:nl.nn.adapterframework.extensions.tibco.SendTibcoMessage.java

public String doPipeWithTimeoutGuarded(Object input, IPipeLineSession session) throws PipeRunException {
    Connection connection = null;
    Session jSession = null;
    MessageProducer msgProducer = null;// w  w w.  j  av  a  2  s.c o m
    Destination destination = null;

    String url_work;
    String authAlias_work;
    String userName_work;
    String password_work;
    String queueName_work;
    String messageProtocol_work;
    int replyTimeout_work;
    String soapAction_work;

    String result = null;

    ParameterValueList pvl = null;
    if (getParameterList() != null) {
        ParameterResolutionContext prc = new ParameterResolutionContext((String) input, session);
        try {
            pvl = prc.getValues(getParameterList());
        } catch (ParameterException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "exception on extracting parameters", e);
        }
    }

    url_work = getParameterValue(pvl, "url");
    if (url_work == null) {
        url_work = getUrl();
    }
    authAlias_work = getParameterValue(pvl, "authAlias");
    if (authAlias_work == null) {
        authAlias_work = getAuthAlias();
    }
    userName_work = getParameterValue(pvl, "userName");
    if (userName_work == null) {
        userName_work = getUserName();
    }
    password_work = getParameterValue(pvl, "password");
    if (password_work == null) {
        password_work = getPassword();
    }
    queueName_work = getParameterValue(pvl, "queueName");
    if (queueName_work == null) {
        queueName_work = getQueueName();
    }
    messageProtocol_work = getParameterValue(pvl, "messageProtocol");
    if (messageProtocol_work == null) {
        messageProtocol_work = getMessageProtocol();
    }
    String replyTimeout_work_str = getParameterValue(pvl, "replyTimeout");
    if (replyTimeout_work_str == null) {
        replyTimeout_work = getReplyTimeout();
    } else {
        replyTimeout_work = Integer.parseInt(replyTimeout_work_str);
    }
    soapAction_work = getParameterValue(pvl, "soapAction");
    if (soapAction_work == null)
        soapAction_work = getSoapAction();

    if (StringUtils.isEmpty(soapAction_work) && !StringUtils.isEmpty(queueName_work)) {
        String[] q = queueName_work.split("\\.");
        if (q.length > 0) {
            if (q[0].equalsIgnoreCase("P2P") && q.length >= 4) {
                soapAction_work = q[3];
            } else if (q[0].equalsIgnoreCase("ESB") && q.length == 8) {
                soapAction_work = q[5] + "_" + q[6];
            } else if (q[0].equalsIgnoreCase("ESB") && q.length > 8) {
                soapAction_work = q[6] + "_" + q[7];
            }
        }
    }

    if (StringUtils.isEmpty(soapAction_work)) {
        log.debug(getLogPrefix(session) + "deriving default soapAction");
        try {
            URL resource = ClassUtils.getResourceURL(this, "/xml/xsl/esb/soapAction.xsl");
            TransformerPool tp = new TransformerPool(resource, true);
            soapAction_work = tp.transform(input.toString(), null);
        } catch (Exception e) {
            log.error(getLogPrefix(session) + "failed to execute soapAction.xsl");
        }
    }

    if (messageProtocol_work == null) {
        throw new PipeRunException(this, getLogPrefix(session) + "messageProtocol must be set");
    }
    if (!messageProtocol_work.equalsIgnoreCase(REQUEST_REPLY)
            && !messageProtocol_work.equalsIgnoreCase(FIRE_AND_FORGET)) {
        throw new PipeRunException(this, getLogPrefix(session) + "illegal value for messageProtocol ["
                + messageProtocol_work + "], must be '" + REQUEST_REPLY + "' or '" + FIRE_AND_FORGET + "'");
    }

    CredentialFactory cf = new CredentialFactory(authAlias_work, userName_work, password_work);
    try {
        TibjmsAdmin admin;
        try {
            admin = TibcoUtils.getActiveServerAdmin(url_work, cf);
        } catch (TibjmsAdminException e) {
            log.debug(getLogPrefix(session) + "caught exception", e);
            admin = null;
        }
        if (admin != null) {
            QueueInfo queueInfo;
            try {
                queueInfo = admin.getQueue(queueName_work);
            } catch (Exception e) {
                throw new PipeRunException(this, getLogPrefix(session) + " exception on getting queue info", e);
            }
            if (queueInfo == null) {
                throw new PipeRunException(this,
                        getLogPrefix(session) + " queue [" + queueName_work + "] does not exist");
            }

            try {
                admin.close();
            } catch (TibjmsAdminException e) {
                log.warn(getLogPrefix(session) + "exception on closing Tibjms Admin", e);
            }
        }

        ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(url_work);
        connection = factory.createConnection(cf.getUsername(), cf.getPassword());
        jSession = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        destination = jSession.createQueue(queueName_work);

        msgProducer = jSession.createProducer(destination);
        TextMessage msg = jSession.createTextMessage();
        msg.setText(input.toString());
        Destination replyQueue = null;
        if (messageProtocol_work.equalsIgnoreCase(REQUEST_REPLY)) {
            replyQueue = jSession.createTemporaryQueue();
            msg.setJMSReplyTo(replyQueue);
            msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
            msgProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            msgProducer.setTimeToLive(replyTimeout_work);
        } else {
            msg.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
            msgProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
        }
        if (StringUtils.isNotEmpty(soapAction_work)) {
            log.debug(
                    getLogPrefix(session) + "setting [SoapAction] property to value [" + soapAction_work + "]");
            msg.setStringProperty("SoapAction", soapAction_work);
        }
        msgProducer.send(msg);
        if (log.isDebugEnabled()) {
            log.debug(getLogPrefix(session) + "sent message [" + msg.getText() + "] " + "to ["
                    + msgProducer.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] "
                    + "correlationID [" + msg.getJMSCorrelationID() + "] " + "replyTo [" + msg.getJMSReplyTo()
                    + "]");
        } else {
            if (log.isInfoEnabled()) {
                log.info(getLogPrefix(session) + "sent message to [" + msgProducer.getDestination() + "] "
                        + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID ["
                        + msg.getJMSCorrelationID() + "] " + "replyTo [" + msg.getJMSReplyTo() + "]");
            }
        }
        if (messageProtocol_work.equalsIgnoreCase(REQUEST_REPLY)) {
            String replyCorrelationId = msg.getJMSMessageID();
            MessageConsumer msgConsumer = jSession.createConsumer(replyQueue,
                    "JMSCorrelationID='" + replyCorrelationId + "'");
            log.debug(getLogPrefix(session) + "] start waiting for reply on [" + replyQueue + "] selector ["
                    + replyCorrelationId + "] for [" + replyTimeout_work + "] ms");
            try {
                connection.start();
                Message rawReplyMsg = msgConsumer.receive(replyTimeout_work);
                if (rawReplyMsg == null) {
                    throw new PipeRunException(this,
                            getLogPrefix(session) + "did not receive reply on [" + replyQueue
                                    + "] replyCorrelationId [" + replyCorrelationId + "] within ["
                                    + replyTimeout_work + "] ms");
                }
                TextMessage replyMsg = (TextMessage) rawReplyMsg;
                result = replyMsg.getText();
            } finally {
            }

        } else {
            result = msg.getJMSMessageID();
        }
    } catch (JMSException e) {
        throw new PipeRunException(this, getLogPrefix(session) + " exception on sending message to Tibco queue",
                e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                log.warn(getLogPrefix(session) + "exception on closing connection", e);
            }
        }
    }
    return result;
}

From source file:nl.nn.adapterframework.jms.JMSFacade.java

public TextMessage createTextMessage(Session session, String correlationID, String message)
        throws javax.naming.NamingException, JMSException {
    TextMessage textMessage = null;//from  ww w . j ava2  s  .c  om
    textMessage = session.createTextMessage();
    if (null != correlationID) {
        if (correlationIdMaxLength >= 0) {
            int cidlen;
            if (correlationID.startsWith(correlationIdToHexPrefix)) {
                cidlen = correlationID.length() - correlationIdToHexPrefix.length();
            } else {
                cidlen = correlationID.length();
            }
            if (cidlen > correlationIdMaxLength) {
                correlationID = correlationIdToHexPrefix
                        + correlationID.substring(correlationID.length() - correlationIdMaxLength);
                if (log.isDebugEnabled())
                    log.debug("correlationId shortened to [" + correlationID + "]");
            }
        }
        if (correlationIdToHex && correlationID.startsWith(correlationIdToHexPrefix)) {
            String hexCorrelationID = correlationIdToHexPrefix;
            int i;
            for (i = correlationIdToHexPrefix.length(); i < correlationID.length(); i++) {
                int c = correlationID.charAt(i);
                hexCorrelationID += Integer.toHexString(c);
            }
            ;
            correlationID = hexCorrelationID;
            if (log.isDebugEnabled())
                log.debug("correlationId changed, based on hexidecimal values, to [" + correlationID + "]");
        }
        textMessage.setJMSCorrelationID(correlationID);
    }
    textMessage.setText(message);
    return textMessage;
}