Home » openejb-3.1.2-src » org.superbiz.moviefun » [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   package org.superbiz.moviefun;
   18   
   19   import javax.annotation.Resource;
   20   import javax.ejb.Stateless;
   21   import javax.jms.Connection;
   22   import javax.jms.ConnectionFactory;
   23   import javax.jms.DeliveryMode;
   24   import javax.jms.JMSException;
   25   import javax.jms.MessageProducer;
   26   import javax.jms.Session;
   27   import javax.jms.TextMessage;
   28   import javax.jms.Topic;
   29   
   30   @Stateless
   31   public class NotifierImpl implements Notifier {
   32   
   33       @Resource
   34       private ConnectionFactory connectionFactory;
   35   
   36       @Resource(name = "notifications")
   37       private Topic notificationsTopic;
   38   
   39       public void notify(String message) {
   40           try {
   41               Connection connection = null;
   42               Session session = null;
   43   
   44               try {
   45                   connection = connectionFactory.createConnection();
   46                   connection.start();
   47   
   48                   // Create a Session
   49                   session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   50   
   51                   // Create a MessageProducer from the Session to the Topic or Queue
   52                   MessageProducer producer = session.createProducer(notificationsTopic);
   53                   producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
   54   
   55                   // Create a message
   56                   TextMessage textMessage = session.createTextMessage(message);
   57   
   58                   // Tell the producer to send the message
   59                   producer.send(textMessage);
   60               } finally {
   61                   // Clean up
   62                   if (session != null) session.close();
   63                   if (connection != null) connection.close();
   64               }
   65           } catch (JMSException e) {
   66               throw new IllegalStateException(e);
   67           }
   68   
   69       }
   70   
   71   }

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