Search in sources :

Example 6 with Session

use of javax.jms.Session in project storm by apache.

the class JmsSpoutTest method sendMessage.

public Message sendMessage(ConnectionFactory connectionFactory, Destination destination) throws JMSException {
    Session mySess = connectionFactory.createConnection().createSession(false, Session.CLIENT_ACKNOWLEDGE);
    MessageProducer producer = mySess.createProducer(destination);
    TextMessage msg = mySess.createTextMessage();
    msg.setText("Hello World");
    LOG.info("Sending Message: {}", msg.getText());
    producer.send(msg);
    return msg;
}
Also used : MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 7 with Session

use of javax.jms.Session in project hive by apache.

the class TestMsgBusConnection method connectClient.

private void connectClient() throws JMSException {
    ConnectionFactory connFac = new ActiveMQConnectionFactory("tcp://localhost:61616");
    Connection conn = connFac.createConnection();
    conn.start();
    Session session = conn.createSession(true, Session.SESSION_TRANSACTED);
    Destination hcatTopic = session.createTopic("planetlab.hcat");
    consumer = session.createConsumer(hcatTopic);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) Destination(javax.jms.Destination) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) Session(javax.jms.Session)

Example 8 with Session

use of javax.jms.Session in project javaee7-samples by javaee-samples.

the class ClassicMessageReceiver method receiveMessage.

public String receiveMessage() {
    String response = null;
    Connection connection = null;
    try {
        connection = connectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer messageConsumer = session.createConsumer(demoQueue);
        Message message = messageConsumer.receive(5000);
        response = message.getBody(String.class);
    } catch (JMSException ex) {
        ex.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException ex) {
                ex.printStackTrace();
            }
        }
    }
    return response;
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 9 with Session

use of javax.jms.Session in project quickstarts by jboss-switchyard.

the class CamelJMSBindingTest method sendTextToQueue.

private void sendTextToQueue(final String text, final String queueName) throws Exception {
    InitialContext initialContext = null;
    Connection connection = null;
    Session session = null;
    MessageProducer producer = null;
    try {
        initialContext = new InitialContext();
        final Queue testQueue = (Queue) initialContext.lookup(queueName);
        final ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
        connection = connectionFactory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(testQueue);
        producer.send(session.createTextMessage(text));
    } finally {
        if (producer != null) {
            producer.close();
        }
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
        if (initialContext != null) {
            initialContext.close();
        }
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) Session(javax.jms.Session)

Example 10 with Session

use of javax.jms.Session in project quickstarts by jboss-switchyard.

the class JMSClient method sendToActiveMQ.

private static void sendToActiveMQ() throws Exception {
    ConnectionFactory cf = new ActiveMQConnectionFactory(AMQ_USER, AMQ_PASSWD, AMQ_BROKER_URL);
    Connection conn = cf.createConnection();
    try {
        Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MessageProducer producer = session.createProducer(session.createQueue(QUEUE_NAME));
        BufferedReader reader = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(MESSAGE_PAYLOAD)));
        StringBuilder buf = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            buf.append(line);
        }
        reader.close();
        Message message = session.createTextMessage(buf.toString());
        producer.send(message);
        System.out.println("Message sent. Please see server console output");
    } finally {
        conn.close();
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) InputStreamReader(java.io.InputStreamReader) Message(javax.jms.Message) Connection(javax.jms.Connection) BufferedReader(java.io.BufferedReader) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Aggregations

Session (javax.jms.Session)231 MessageProducer (javax.jms.MessageProducer)106 Test (org.junit.Test)98 Connection (javax.jms.Connection)88 Message (javax.jms.Message)73 TextMessage (javax.jms.TextMessage)69 JMSException (javax.jms.JMSException)58 MessageConsumer (javax.jms.MessageConsumer)53 Topic (javax.jms.Topic)36 ConnectionFactory (javax.jms.ConnectionFactory)31 Destination (javax.jms.Destination)27 Queue (javax.jms.Queue)26 ObjectMessage (javax.jms.ObjectMessage)24 StubTextMessage (org.springframework.jms.StubTextMessage)20 QueueSession (javax.jms.QueueSession)16 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)16 MapMessage (javax.jms.MapMessage)12 MessageCreator (org.springframework.jms.core.MessageCreator)12 BytesMessage (javax.jms.BytesMessage)11 TopicSession (javax.jms.TopicSession)9