Home » openejb-3.1.2-src » org.superbiz.injection.jms » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   //START SNIPPET: code
   18   package org.superbiz.injection.jms;
   19   
   20   import javax.annotation.Resource;
   21   import javax.ejb.Stateless;
   22   import javax.jms.Connection;
   23   import javax.jms.ConnectionFactory;
   24   import javax.jms.DeliveryMode;
   25   import javax.jms.JMSException;
   26   import javax.jms.MessageConsumer;
   27   import javax.jms.MessageProducer;
   28   import javax.jms.Queue;
   29   import javax.jms.Session;
   30   import javax.jms.TextMessage;
   31   
   32   @Stateless
   33   public class MessagingBean implements MessagingLocal {
   34   
   35       @Resource
   36       private ConnectionFactory connectionFactory;
   37   
   38       @Resource
   39       private Queue chatQueue;
   40   
   41   
   42       public void sendMessage(String text) throws JMSException {
   43   
   44           Connection connection = null;
   45           Session session = null;
   46   
   47           try {
   48               connection = connectionFactory.createConnection();
   49               connection.start();
   50   
   51               // Create a Session
   52               session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   53   
   54               // Create a MessageProducer from the Session to the Topic or Queue
   55               MessageProducer producer = session.createProducer(chatQueue);
   56               producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
   57   
   58               // Create a message
   59               TextMessage message = session.createTextMessage(text);
   60   
   61               // Tell the producer to send the message
   62               producer.send(message);
   63           } finally {
   64               // Clean up
   65               if (session != null) session.close();
   66               if (connection != null) connection.close();
   67           }
   68       }
   69   
   70       public String receiveMessage() throws JMSException {
   71   
   72           Connection connection = null;
   73           Session session = null;
   74           MessageConsumer consumer = null;
   75           try {
   76               connection = connectionFactory.createConnection();
   77               connection.start();
   78   
   79               // Create a Session
   80               session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   81   
   82               // Create a MessageConsumer from the Session to the Topic or Queue
   83               consumer = session.createConsumer(chatQueue);
   84   
   85               // Wait for a message
   86               TextMessage message = (TextMessage) consumer.receive(1000);
   87   
   88               return message.getText();
   89           } finally {
   90               if (consumer != null) consumer.close();
   91               if (session != null) session.close();
   92               if (connection != null) connection.close();
   93           }
   94   
   95       }
   96   }
   97   //END SNIPPET: code

Home » openejb-3.1.2-src » org.superbiz.injection.jms » [javadoc | source]