KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > mom > util > BridgePtpModule


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - Bull SA
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Frederic Maistre (Bull SA)
21  * Contributor(s):
22  */

23 package org.objectweb.joram.mom.util;
24
25 import javax.jms.*;
26 import javax.jms.IllegalStateException JavaDoc;
27
28 import java.util.Vector JavaDoc;
29
30
31 /**
32  * The <code>BridgePtpModule</code> class is a bridge module based on the JMS
33  * PTP semantics and classes.
34  */

35 public class BridgePtpModule extends BridgeUnifiedModule
36 {
37   /** Specific PTP producer resource. */
38   private transient QueueSender sender;
39
40
41   /** Constructs a <code>BridgePtpModule</code> module. */
42   public BridgePtpModule()
43   {
44     super();
45   }
46
47  
48   /**
49    * Sends a message to the foreign JMS queue.
50    *
51    * @exception javax.jms.IllegalStateException If the module's state does
52    * not permit message sendings.
53    * @exception javax.jms.MessageFormatException If the MOM message could not
54    * be converted into a foreign JMS message.
55    */

56   public void send(org.objectweb.joram.shared.messages.Message message)
57               throws JMSException
58   {
59     if (! usable)
60       throw new IllegalStateException JavaDoc(notUsableMessage);
61
62     try {
63       sender.send(MessageConverterModule.convert(producerSession, message));
64       acknowledge(message);
65     }
66     catch (javax.jms.MessageFormatException JavaDoc exc) {
67       throw exc;
68     }
69     // Connection failure? Keeping the message for later delivery.
70
catch (javax.jms.JMSException JavaDoc exc) {
71       qout.add(message);
72     }
73   }
74
75   /**
76    * Opens a connection with the foreign JMS server and creates the
77    * JMS PTP resources for interacting with the foreign JMS queue.
78    *
79    * @exception JMSException If the needed JMS resources could not be created.
80    */

81   protected void doConnect() throws JMSException
82   {
83     QueueConnectionFactory queueCnxFact = (QueueConnectionFactory) cnxFact;
84     Queue queue = (Queue) dest;
85
86     if (userName != null && password != null)
87       cnx = queueCnxFact.createQueueConnection(userName, password);
88     else
89       cnx = queueCnxFact.createQueueConnection();
90     cnx.setExceptionListener(this);
91
92     if (clientID != null)
93       cnx.setClientID(clientID);
94
95     producerSession =
96       ((QueueConnection) cnx).createQueueSession(false,
97                                                  Session.AUTO_ACKNOWLEDGE);
98     sender = ((QueueSession) producerSession).createSender(queue);
99
100     consumerSession = ((QueueConnection) cnx).createQueueSession(true, 0);
101     consumer = ((QueueSession) consumerSession).createReceiver(queue,
102                                                                selector);
103   }
104 }
105
Popular Tags