Search in sources :

Example 26 with MessageProducer

use of javax.jms.MessageProducer in project fabric8 by jboss-fuse.

the class DetectingGatewayTest method canDetectTheOpenwireSslProtocol.

@Test
public void canDetectTheOpenwireSslProtocol() throws Exception {
    System.setProperty("javax.net.ssl.trustStore", new File(basedir(), "src/test/resources/client.ks").getCanonicalPath());
    System.setProperty("javax.net.ssl.trustStorePassword", "password");
    System.setProperty("javax.net.ssl.trustStoreType", "jks");
    DetectingGateway gateway = createGateway();
    String url = "ssl://localhost:" + gateway.getBoundPort() + "?wireFormat.host=broker0";
    final ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
    Connection connection = factory.createConnection();
    connection.start();
    assertEquals(1, gateway.getSuccessfulConnectionAttempts());
    assertEquals(1, gateway.getConnectedClients().length);
    assertConnectedToBroker(0);
    Thread.sleep(6000);
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination test = new ActiveMQQueue("TEST");
    MessageProducer producer = session.createProducer(test);
    producer.send(session.createTextMessage("Hello"));
    connection.close();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Destination(javax.jms.Destination) Connection(javax.jms.Connection) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) File(java.io.File) Session(javax.jms.Session) Test(org.junit.Test)

Example 27 with MessageProducer

use of javax.jms.MessageProducer in project fabric8 by jboss-fuse.

the class DetectingGatewayVirtualHostTest method canDetectTheOpenwireSslProtocol.

@Test
public void canDetectTheOpenwireSslProtocol() throws Exception {
    System.setProperty("javax.net.ssl.trustStore", new File(basedir(), "src/test/resources/client.ks").getCanonicalPath());
    System.setProperty("javax.net.ssl.trustStorePassword", "password");
    System.setProperty("javax.net.ssl.trustStoreType", "jks");
    DetectingGateway gateway = createGateway();
    gateway.setConnectionTimeout(9_000_000);
    String url = "ssl://localhost:" + gateway.getBoundPort() + "?wireFormat.host=broker";
    final ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
    Connection connection = factory.createConnection();
    connection.start();
    assertEquals(1, gateway.getSuccessfulConnectionAttempts());
    assertEquals(1, gateway.getConnectedClients().length);
    Thread.sleep(6000);
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination test = new ActiveMQQueue("TEST");
    MessageProducer producer = session.createProducer(test);
    producer.send(session.createTextMessage("Hello"));
    connection.close();
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Destination(javax.jms.Destination) Connection(javax.jms.Connection) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) File(java.io.File) Session(javax.jms.Session) Test(org.junit.Test)

Example 28 with MessageProducer

use of javax.jms.MessageProducer in project iaf by ibissource.

the class JMSFacade method send.

public String send(Session session, Destination dest, String correlationId, String message, String messageType, long timeToLive, int deliveryMode, int priority, boolean ignoreInvalidDestinationException, Map properties) throws NamingException, JMSException, SenderException {
    TextMessage msg = createTextMessage(session, correlationId, message);
    MessageProducer mp;
    try {
        if (useJms102()) {
            if ((session instanceof TopicSession) && (dest instanceof Topic)) {
                mp = getTopicPublisher((TopicSession) session, (Topic) dest);
            } else {
                if ((session instanceof QueueSession) && (dest instanceof Queue)) {
                    mp = getQueueSender((QueueSession) session, (Queue) dest);
                } else {
                    throw new SenderException("classes of Session [" + session.getClass().getName() + "] and Destination [" + dest.getClass().getName() + "] do not match (Queue vs Topic)");
                }
            }
        } else {
            mp = session.createProducer(dest);
        }
    } catch (InvalidDestinationException e) {
        if (ignoreInvalidDestinationException) {
            log.warn("queue [" + dest + "] doesn't exist");
            return null;
        } else {
            throw e;
        }
    }
    if (messageType != null) {
        msg.setJMSType(messageType);
    }
    if (deliveryMode > 0) {
        msg.setJMSDeliveryMode(deliveryMode);
        mp.setDeliveryMode(deliveryMode);
    }
    if (priority >= 0) {
        msg.setJMSPriority(priority);
        mp.setPriority(priority);
    }
    if (timeToLive > 0) {
        mp.setTimeToLive(timeToLive);
    }
    if (properties != null) {
        for (Iterator it = properties.keySet().iterator(); it.hasNext(); ) {
            String key = (String) it.next();
            Object value = properties.get(key);
            log.debug("setting property [" + name + "] to value [" + value + "]");
            msg.setObjectProperty(key, value);
        }
    }
    String result = send(mp, msg, ignoreInvalidDestinationException);
    mp.close();
    return result;
}
Also used : TopicSession(javax.jms.TopicSession) Iterator(java.util.Iterator) InvalidDestinationException(javax.jms.InvalidDestinationException) INamedObject(nl.nn.adapterframework.core.INamedObject) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) SenderException(nl.nn.adapterframework.core.SenderException) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) QueueSession(javax.jms.QueueSession)

Example 29 with MessageProducer

use of javax.jms.MessageProducer in project iaf by ibissource.

the class SendTibcoMessage method doPipeWithTimeoutGuarded.

public String doPipeWithTimeoutGuarded(Object input, IPipeLineSession session) throws PipeRunException {
    Connection connection = null;
    Session jSession = null;
    MessageProducer msgProducer = null;
    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 = TransformerPool.getInstance(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;
}
Also used : QueueInfo(com.tibco.tibjms.admin.QueueInfo) Destination(javax.jms.Destination) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) JMSException(javax.jms.JMSException) URL(java.net.URL) TransformerPool(nl.nn.adapterframework.util.TransformerPool) ConnectionFactory(javax.jms.ConnectionFactory) ParameterException(nl.nn.adapterframework.core.ParameterException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) MessageConsumer(javax.jms.MessageConsumer) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) Connection(javax.jms.Connection) TibjmsAdmin(com.tibco.tibjms.admin.TibjmsAdmin) PipeRunException(nl.nn.adapterframework.core.PipeRunException) JMSException(javax.jms.JMSException) TibjmsAdminException(com.tibco.tibjms.admin.TibjmsAdminException) ParameterException(nl.nn.adapterframework.core.ParameterException) TibjmsAdminException(com.tibco.tibjms.admin.TibjmsAdminException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) IPipeLineSession(nl.nn.adapterframework.core.IPipeLineSession)

Example 30 with MessageProducer

use of javax.jms.MessageProducer in project wildfly-camel by wildfly-extras.

the class SJMS2IntegrationTest method sendMessage.

private void sendMessage(Connection connection, String jndiName, String message) throws Exception {
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = (Destination) initialctx.lookup(jndiName);
    MessageProducer producer = session.createProducer(destination);
    TextMessage msg = session.createTextMessage(message);
    producer.send(msg);
    connection.start();
}
Also used : Destination(javax.jms.Destination) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Aggregations

MessageProducer (javax.jms.MessageProducer)1512 Session (javax.jms.Session)1302 MessageConsumer (javax.jms.MessageConsumer)987 Connection (javax.jms.Connection)915 TextMessage (javax.jms.TextMessage)888 Test (org.junit.Test)850 Message (javax.jms.Message)646 Queue (javax.jms.Queue)404 JMSException (javax.jms.JMSException)213 Destination (javax.jms.Destination)190 ConnectionFactory (javax.jms.ConnectionFactory)147 BytesMessage (javax.jms.BytesMessage)140 Topic (javax.jms.Topic)140 TopicSession (javax.jms.TopicSession)135 TopicConnection (javax.jms.TopicConnection)115 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)108 ObjectMessage (javax.jms.ObjectMessage)98 CountDownLatch (java.util.concurrent.CountDownLatch)91 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)90 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)88