Search in sources :

Example 36 with TextMessage

use of javax.jms.TextMessage in project oxAuth by GluuFederation.

the class ApplicationAuditLogger method loggingThroughJMS.

private void loggingThroughJMS(OAuth2AuditLog oAuth2AuditLog) {
    QueueConnection connection = null;
    try {
        connection = pooledConnectionFactory.createQueueConnection();
        connection.start();
        QueueSession session = connection.createQueueSession(transacted, ACK_MODE);
        MessageProducer producer = session.createProducer(session.createQueue(CLIENT_QUEUE_NAME));
        TextMessage txtMessage = session.createTextMessage();
        txtMessage.setText(ServerUtil.asPrettyJson(oAuth2AuditLog));
        producer.send(txtMessage);
    } catch (JMSException e) {
        log.error("Can't send message", e);
    } catch (IOException e) {
        log.error("Can't serialize the audit log", e);
    } catch (Exception e) {
        log.error("Can't send message, please check your activeMQ configuration.", e);
    } finally {
        if (connection == null)
            return;
        try {
            connection.close();
        } catch (JMSException e) {
            log.error("Can't close connection.");
        }
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) IOException(java.io.IOException) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 37 with TextMessage

use of javax.jms.TextMessage in project adempiere by adempiere.

the class TopicExportProcessor method sendJMSMessage.

private void sendJMSMessage(String host, int port, String msg, String protocol, String topicName, String clientID, String userName, String password, int timeToLive, boolean isDeliveryModePersistent) throws JMSException {
    // Create a ConnectionFactory
    // network protocol (tcp, ...) set as EXP_ProcessorParameter
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(protocol + "://" + host + ":" + port);
    Connection connection = null;
    Session session = null;
    try {
        // Create a Connection
        if (userName != null && password != null) {
            connection = connectionFactory.createConnection(userName, password);
        } else {
            connection = connectionFactory.createConnection();
        }
        // connection.setClientID( clientID ); Commented by Victor as he had issue!
        connection.start();
        // Create a Session
        //TODO - Trifon could be EXP_ProcessorParameter
        session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
        // Create the destination (Topic or Queue)
        Destination destination = session.createTopic(topicName);
        // Create a MessageProducer from the Session to the Topic or Queue
        MessageProducer producer = session.createProducer(destination);
        // EXP_ProcessorParameter
        producer.setTimeToLive(timeToLive);
        if (isDeliveryModePersistent) {
            // EXP_ProcessorParameter	
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            // EXP_ProcessorParameter
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        // How to send to multiple destinations.
        //MessageProducer producer = session.createProducer(null);
        //producer.send(someDestination, message);
        //producer.send(anotherDestination, message);
        // Create a message
        TextMessage message = session.createTextMessage(msg);
        // Tell the producer to send the message
        try {
            producer.send(message);
            session.commit();
            log.info("JMS Message sent!");
        } catch (JMSException ex) {
            session.rollback();
            log.info("JMS Can't send the message!");
            throw ex;
        }
    } finally {
        // Clean up
        if (session != null) {
            try {
                session.close();
            } catch (JMSException ex) {
            /* ignored */
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException ex) {
            /* ignored */
            }
        }
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Destination(javax.jms.Destination) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 38 with TextMessage

use of javax.jms.TextMessage in project karaf by apache.

the class ArtemisDestinationSourceFactory method getNames.

private List<String> getNames(Connection connection, DestinationSource.DestinationType type) {
    try {
        QueueSession session = ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue managementQueue = session.createQueue("activemq.management");
        QueueRequestor requestor = new QueueRequestor(session, managementQueue);
        connection.start();
        TextMessage m = session.createTextMessage();
        m.setStringProperty("_AMQ_ResourceName", "broker");
        m.setStringProperty("_AMQ_OperationName", "getQueueNames");
        String routing = type == DestinationSource.DestinationType.Queue ? "ANYCAST" : "MULTICAST";
        m.setText("[\"" + routing + "\"]");
        Message reply = requestor.request(m);
        String json = ((TextMessage) reply).getText();
        List<?> array = (List<?>) JsonReader.read(new StringReader(json));
        return (List<String>) array.get(0);
    } catch (Exception e) {
        return Collections.emptyList();
    }
}
Also used : QueueRequestor(javax.jms.QueueRequestor) QueueConnection(javax.jms.QueueConnection) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) StringReader(java.io.StringReader) List(java.util.List) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) JMSException(javax.jms.JMSException)

Example 39 with TextMessage

use of javax.jms.TextMessage in project jmeter by apache.

the class JMSSampler method sample.

/**
     * {@inheritDoc}
     */
@Override
public SampleResult sample(Entry entry) {
    SampleResult res = new SampleResult();
    res.setSampleLabel(getName());
    res.setSamplerData(getContent());
    // Assume failure
    res.setSuccessful(false);
    res.setDataType(SampleResult.TEXT);
    res.sampleStart();
    try {
        TextMessage msg = createMessage();
        if (isOneway()) {
            int deliveryMode = isNonPersistent() ? DeliveryMode.NON_PERSISTENT : DeliveryMode.PERSISTENT;
            producer.send(msg, deliveryMode, Integer.parseInt(getPriority()), Long.parseLong(getExpiration()));
            res.setRequestHeaders(Utils.messageProperties(msg));
            res.setResponseOK();
            res.setResponseData("Oneway request has no response data", null);
        } else {
            if (!useTemporyQueue()) {
                msg.setJMSReplyTo(receiveQueue);
            }
            Message replyMsg = executor.sendAndReceive(msg, isNonPersistent() ? DeliveryMode.NON_PERSISTENT : DeliveryMode.PERSISTENT, Integer.parseInt(getPriority()), Long.parseLong(getExpiration()));
            res.setRequestHeaders(Utils.messageProperties(msg));
            if (replyMsg == null) {
                res.setResponseMessage("No reply message received");
            } else {
                if (replyMsg instanceof TextMessage) {
                    res.setResponseData(((TextMessage) replyMsg).getText(), null);
                } else {
                    res.setResponseData(replyMsg.toString(), null);
                }
                res.setResponseHeaders(Utils.messageProperties(replyMsg));
                res.setResponseOK();
            }
        }
    } catch (Exception e) {
        LOGGER.warn(e.getLocalizedMessage(), e);
        if (thrown != null) {
            res.setResponseMessage(thrown.toString());
        } else {
            res.setResponseMessage(e.getLocalizedMessage());
        }
    }
    res.sampleEnd();
    return res;
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) SampleResult(org.apache.jmeter.samplers.SampleResult) TextMessage(javax.jms.TextMessage) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException)

Example 40 with TextMessage

use of javax.jms.TextMessage in project jmeter by apache.

the class SubscriberSampler method extractContent.

private void extractContent(StringBuilder buffer, StringBuilder propBuffer, Message msg, boolean isLast) {
    if (msg != null) {
        try {
            if (msg instanceof TextMessage) {
                buffer.append(((TextMessage) msg).getText());
            } else if (msg instanceof ObjectMessage) {
                ObjectMessage objectMessage = (ObjectMessage) msg;
                if (objectMessage.getObject() != null) {
                    buffer.append(objectMessage.getObject().getClass());
                } else {
                    buffer.append("object is null");
                }
            } else if (msg instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) msg;
                buffer.append(bytesMessage.getBodyLength() + " bytes received in BytesMessage");
            } else if (msg instanceof MapMessage) {
                MapMessage mapm = (MapMessage) msg;
                // MapNames are Strings
                @SuppressWarnings("unchecked") Enumeration<String> enumb = mapm.getMapNames();
                while (enumb.hasMoreElements()) {
                    String name = enumb.nextElement();
                    Object obj = mapm.getObject(name);
                    buffer.append(name);
                    buffer.append(",");
                    buffer.append(obj.getClass().getCanonicalName());
                    buffer.append(",");
                    buffer.append(obj);
                    buffer.append("\n");
                }
            }
            Utils.messageProperties(propBuffer, msg);
            if (!isLast && !StringUtils.isEmpty(separator)) {
                propBuffer.append(separator);
                buffer.append(separator);
            }
        } catch (JMSException e) {
            log.error(e.getMessage());
        }
    }
}
Also used : Enumeration(java.util.Enumeration) ObjectMessage(javax.jms.ObjectMessage) MapMessage(javax.jms.MapMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException) TextMessage(javax.jms.TextMessage)

Aggregations

TextMessage (javax.jms.TextMessage)231 Test (org.junit.Test)92 Session (javax.jms.Session)75 MessageProducer (javax.jms.MessageProducer)71 Message (javax.jms.Message)70 JMSException (javax.jms.JMSException)64 Connection (javax.jms.Connection)44 Destination (javax.jms.Destination)44 MessageConsumer (javax.jms.MessageConsumer)44 ObjectMessage (javax.jms.ObjectMessage)25 BytesMessage (javax.jms.BytesMessage)22 Queue (javax.jms.Queue)21 QueueSession (javax.jms.QueueSession)20 StubTextMessage (org.springframework.jms.StubTextMessage)18 ConnectionFactory (javax.jms.ConnectionFactory)13 QueueConnection (javax.jms.QueueConnection)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 MapMessage (javax.jms.MapMessage)11 Topic (javax.jms.Topic)11 JMSContext (javax.jms.JMSContext)9