KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cts > jms > ContainerMBox


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cts.jms;
23
24 import java.util.HashMap JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageListener JavaDoc;
28 import javax.jms.QueueConnection JavaDoc;
29 import javax.jms.QueueConnectionFactory JavaDoc;
30 import javax.jms.QueueReceiver JavaDoc;
31 import javax.jms.QueueSession JavaDoc;
32 import javax.jms.Session JavaDoc;
33 import javax.jms.TextMessage JavaDoc;
34 import javax.jms.Queue JavaDoc;
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38
39 import org.jboss.logging.Logger;
40
41 public class ContainerMBox
42   implements MessageListener JavaDoc
43 {
44   public final static String JavaDoc JMS_FACTORY="ConnectionFactory";
45   public final static String JavaDoc QUEUE="queue/testQueue";
46
47   private QueueConnectionFactory JavaDoc qconFactory;
48   private QueueConnection JavaDoc qcon;
49   private QueueSession JavaDoc qsession;
50   private QueueReceiver JavaDoc qreceiver;
51   private Queue JavaDoc queue;
52
53   private Logger log;
54
55   public static final String JavaDoc EJB_CREATE_MSG = "EJB_CREATE_MSG";
56   public static final String JavaDoc EJB_POST_CREATE_MSG = "EJB_POST_CREATE_MSG";
57   public static final String JavaDoc EJB_ACTIVATE_MSG = "EJB_ACTIVATE_MSG";
58   public static final String JavaDoc EJB_PASSIVATE_MSG = "EJB_PASSIVATE_MSG";
59   public static final String JavaDoc EJB_REMOVE_MSG = "EJB_REMOVE_MSG";
60   public static final String JavaDoc EJB_LOAD_MSG = "EJB_LOAD_MSG";
61   public static final String JavaDoc EJB_STORE_MSG = "EJB_STORE_MSG";
62   public static final String JavaDoc SET_ENTITY_CONTEXT_MSG = "SET_ENTITY_CONTEXT_MSG";
63   public static final String JavaDoc UNSET_ENTITY_CONTEXT_MSG = "UNSET_ENTITY_CONTEXT_MSG";
64
65   private HashMap JavaDoc messageList = new HashMap JavaDoc( );
66
67   public ContainerMBox ( )
68   {
69     log = Logger.getLogger(getClass().getName());
70     try
71     {
72        init( new InitialContext JavaDoc(), QUEUE );
73     }
74     catch(Exception JavaDoc ex)
75     {
76        log.error("MBox could not get initial context", ex);
77     }
78   }
79
80   // MessageListener interface
81
public void onMessage(Message JavaDoc msg)
82   {
83     try
84     {
85       String JavaDoc msgText;
86       if (msg instanceof TextMessage JavaDoc)
87       {
88         msgText = ((TextMessage JavaDoc)msg).getText();
89       }
90       else
91       {
92         msgText = msg.toString();
93       }
94
95       log.debug("[BEAN MESSAGE]: "+ msgText );
96       messageList.put(msgText, "msg" );
97     }
98     catch (JMSException JavaDoc jmse)
99     {
100       log.error("problem receiving MBox message", jmse);
101     }
102   }
103
104   /**
105    * Create all the necessary objects for receiving
106    * messages from a JMS queue.
107    */

108   public void init(Context JavaDoc ctx, String JavaDoc queueName)
109        throws NamingException JavaDoc, JMSException JavaDoc
110   {
111     qconFactory = (QueueConnectionFactory JavaDoc) ctx.lookup(JMS_FACTORY);
112     qcon = qconFactory.createQueueConnection();
113     qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
114     try
115     {
116       queue = (Queue JavaDoc) ctx.lookup(queueName);
117     }
118     catch (NamingException JavaDoc ne)
119     {
120       queue = qsession.createQueue(queueName);
121       ctx.bind(queueName, queue);
122     }
123     qreceiver = qsession.createReceiver(queue);
124     qreceiver.setMessageListener(this);
125     qcon.start();
126   }
127
128   /**
129    * Close JMS objects.
130    */

131   public void close()
132        throws JMSException JavaDoc
133   {
134     qreceiver.close();
135     qsession.close();
136     qcon.close();
137   }
138
139   public boolean messageReceived( String JavaDoc message )
140   {
141       return messageList.containsKey(message);
142   }
143
144   public void clearMessages( )
145   {
146       messageList = null;
147       messageList = new HashMap JavaDoc();
148   }
149
150 }
151
152
153
154
155
156
157
Popular Tags