Search in sources :

Example 1 with QueueConnection

use of javax.jms.QueueConnection 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 2 with QueueConnection

use of javax.jms.QueueConnection 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 3 with QueueConnection

use of javax.jms.QueueConnection in project opennms by OpenNMS.

the class DaemonContextIT method canUseEmbeddedActiveMQBroker.

/**
 * Verifies that the embedded ActiveMQ broker bootstraps successfully
 * and is accessible using the provided connection factory.
 */
@Test
public void canUseEmbeddedActiveMQBroker() throws Throwable {
    QueueConnection connection = activeMQConnectionFactory.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
    TextMessage message = session.createTextMessage();
    message.setText("ping");
    QueueSender sender = session.createSender(session.createQueue("pong"));
    sender.send(message);
}
Also used : QueueConnection(javax.jms.QueueConnection) QueueSender(javax.jms.QueueSender) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 4 with QueueConnection

use of javax.jms.QueueConnection in project Payara by payara.

the class UnitTest method testTimer.

@Test
public void testTimer() throws Exception {
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    try {
        InitialContext ic = new InitialContext();
        QueueConnectionFactory qcf = (QueueConnectionFactory) ic.lookup("jms/TestQueueConnectionFactory");
        javax.jms.Queue queue = (javax.jms.Queue) ic.lookup("jms/TestQueue");
        queueConnection = qcf.createQueueConnection();
        queueConnection.start();
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueSender sender = queueSession.createSender(queue);
        String str = "Hi From BHAVANI";
        TextMessage msg = queueSession.createTextMessage(str);
        sender.send(msg);
        Thread.sleep(5000);
        byte[] message = new byte[msg.getText().length()];
        File savedFile = new File(System.getProperty("java.io.tmpdir"), "embedded_mdb_onmessage.txt");
        FileInputStream is = new FileInputStream(savedFile);
        is.read(message);
        String savedMsg = new String(message);
        if (!savedMsg.equals(str)) {
            throw new Exception("Sent message [" + str + " ] does not match the received message [" + savedMsg + "]");
        } else {
            System.out.println("Sent message [" + str + " ]  matches the received message [" + savedMsg + "]");
        }
        savedFile.delete();
    } finally {
        try {
            queueSession.close();
            queueConnection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
Also used : QueueConnectionFactory(javax.jms.QueueConnectionFactory) InitialContext(javax.naming.InitialContext) FileInputStream(java.io.FileInputStream) QueueConnection(javax.jms.QueueConnection) QueueSender(javax.jms.QueueSender) File(java.io.File) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 5 with QueueConnection

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

the class MessagingSource method createSession.

public Session createSession(boolean transacted, int acknowledgeMode) throws IbisException {
    Connection connection = null;
    ;
    Session session;
    try {
        connection = getConnection();
    } catch (JMSException e) {
        throw new JmsException("could not obtain Connection", e);
    }
    try {
        // if (log.isDebugEnabled()) log.debug(getLogPrefix()+"creating Session, openSessionCount before ["+openSessionCount.getValue()+"]");
        if (useJms102()) {
            if (connection instanceof QueueConnection) {
                session = ((QueueConnection) connection).createQueueSession(transacted, acknowledgeMode);
            } else {
                session = ((TopicConnection) connection).createTopicSession(transacted, acknowledgeMode);
            }
        } else {
            session = connection.createSession(transacted, acknowledgeMode);
        }
        openSessionCount.increase();
        if (connectionsArePooled()) {
            connectionTable.put(session, connection);
        }
        return session;
    } catch (JMSException e) {
        releaseConnection(connection);
        throw new JmsException("could not create Session", e);
    }
}
Also used : QueueConnection(javax.jms.QueueConnection) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Aggregations

QueueConnection (javax.jms.QueueConnection)77 QueueSession (javax.jms.QueueSession)53 Test (org.junit.Test)41 TextMessage (javax.jms.TextMessage)36 Queue (javax.jms.Queue)33 Message (javax.jms.Message)26 MessageProducer (javax.jms.MessageProducer)20 QueueConnectionFactory (javax.jms.QueueConnectionFactory)20 Session (javax.jms.Session)17 JMSException (javax.jms.JMSException)16 InitialContext (javax.naming.InitialContext)15 QueueSender (javax.jms.QueueSender)14 XAQueueConnection (javax.jms.XAQueueConnection)14 ActiveMQRAConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory)14 ActiveMQRAConnectionFactoryImpl (org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl)14 ActiveMQRAManagedConnectionFactory (org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory)14 QueueReceiver (javax.jms.QueueReceiver)13 MessageConsumer (javax.jms.MessageConsumer)12 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)9 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)6