1 /*
2 * Copyright (c) 2001 Peter Antman Tim <peter.antman@tim.se>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package javaclients.j2ee.k2;
19
20 import javax.naming.InitialContext;
21 import javax.naming.NamingException;
22
23 import javax.ejb.MessageDrivenBean;
24 import javax.ejb.MessageDrivenContext;
25 import javax.ejb.EJBException;
26
27 import javax.jms.MessageListener;
28 import javax.jms.Message;
29 import javax.jms.TextMessage;
30 import javax.jms.JMSException;
31
32 import javax.resource.ResourceException;
33
34 import org.xmlBlaster.j2ee.k2.client.*;
35
36 import org.xmlBlaster.util.XmlBlasterException;
37 import org.xmlBlaster.util.MsgUnit;
38 /**
39 * MessageBeanImpl.java
40 *
41 *
42 * Created: Sat Nov 25 18:07:50 2000
43 */
44
45 public class JmsAdapter implements MessageDrivenBean, MessageListener{
46 private MessageDrivenContext ctx = null;
47 private BlasterConnectionFactory factory = null;
48 public JmsAdapter() {
49
50 }
51 public void setMessageDrivenContext(MessageDrivenContext ctx)
52 throws EJBException {
53 this.ctx = ctx;
54 try {
55 factory = (BlasterConnectionFactory)new InitialContext ().lookup ("java:comp/env/xmlBlaster");
56 } catch (NamingException ex) {
57 throw new EJBException ("XmlBlaster not found: "+ex.getMessage ());
58 }catch(Throwable th) {
59 System.err.println("Throwable: " +th);
60 th.printStackTrace();
61 throw new EJBException("Throwable in setContext: " +th);
62 }
63
64 }
65
66 public void ejbCreate() {}
67
68 public void ejbRemove() {ctx=null;}
69
70 public void onMessage(Message message) {
71
72 BlasterConnection con = null;
73 try {
74 // Get message to handle
75 System.err.println("Got message: " + message);
76
77 if (message instanceof TextMessage) {
78 String msg = ((TextMessage)message).getText();
79
80 // Get connection
81 con = factory.getConnection();
82
83 // Construct Blaster Headers - howto hanlde key here?
84 String key ="<key oid=\"" + message.getJMSMessageID() +"\" contentMime=\"text/xml\"></key>";
85 String qos = "<qos></qos>";
86 con.publish( new MsgUnit(key,msg.getBytes(),qos));
87
88 } else {
89 System.err.println("Got message type I cant handle");
90 }
91
92 }catch(ResourceException re) {
93 System.err.println("Resource ex: " +re);
94 re.printStackTrace();
95 } catch(XmlBlasterException be) {
96 System.err.println("Blaster ex: " +be);
97 be.printStackTrace();
98 }catch(JMSException je) {
99 System.err.println("JMSException ex: " +je);
100 je.printStackTrace();
101 }catch(Throwable th) {
102 System.err.println("Throwable: " +th);
103 th.printStackTrace();
104
105 }finally {
106 try {
107 System.err.println("Jms closing con: " + con);
108 if (con != null)
109 con.close ();
110 }
111 catch (Exception ex) {}
112
113 }
114 }
115 } // MessageBeanImpl
syntax highlighted by Code2HTML, v. 0.9.1