Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.sample.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 this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.sample.jms;
   15   
   16   import java.io.Serializable;
   17   import java.util.ArrayList;
   18   import java.util.List;
   19   
   20   import javax.enterprise.context.SessionScoped;
   21   import javax.inject.Inject;
   22   import javax.inject.Named;
   23   import javax.jms.JMSException;
   24   import javax.jms.Message;
   25   import javax.jms.MessageListener;
   26   import javax.jms.QueueConnection;
   27   import javax.jms.QueueReceiver;
   28   import javax.jms.TextMessage;
   29   
   30   import org.apache.webbeans.sample.bindings.JmsBinding;
   31   
   32   @SessionScoped
   33   @Named("receiver")
   34   public class JmsReceiverBean implements MessageListener,Serializable
   35   {
   36       private static final long serialVersionUID = 5704416477431590842L;
   37   
   38       private @Inject @JmsBinding QueueReceiver queueReceiver;
   39       
   40       private @Inject @JmsBinding QueueConnection queueConnection;
   41       
   42       private String message;
   43       
   44       private boolean receive = false;
   45       
   46       private List<String> messages = new ArrayList<String>();
   47   
   48       public String getMessage()
   49       {
   50           return message;
   51       }
   52   
   53       public void setMessage(String message)
   54       {
   55           this.message = message;
   56       }
   57   
   58       public void startReceive()
   59       {
   60           try
   61           {
   62               if(!receive)
   63               {
   64                   this.receive = true;
   65                   this.queueConnection.start();
   66               }
   67               
   68               queueReceiver.setMessageListener(this);
   69           }
   70           catch (JMSException e)
   71           {
   72               e.printStackTrace();
   73           }
   74       }
   75       
   76       @Override
   77       public void onMessage(Message message)
   78       {
   79           TextMessage me = (TextMessage) message;
   80           try
   81           {
   82               this.message = me.getText();
   83               this.messages.add(this.message);
   84           }
   85           catch (JMSException e)
   86           {
   87               e.printStackTrace();
   88           }
   89       }
   90   
   91       public String refresh()
   92       {
   93           return null;
   94       }
   95       
   96       public boolean isReceive()
   97       {
   98           return receive;
   99       }
  100   
  101       public void setReceive(boolean receive)
  102       {
  103           this.receive = receive;
  104       }
  105   
  106       public List<String> getMessages()
  107       {
  108           return messages;
  109       }
  110   
  111       public void setMessages(List<String> messages)
  112       {
  113           this.messages = messages;
  114       }
  115       
  116       
  117   
  118   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.sample.jms » [javadoc | source]