KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > jbi > serviceengine > comm > MessageSender


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.jbi.serviceengine.comm;
24 import com.sun.enterprise.jbi.serviceengine.work.OneWork;
25 import java.util.logging.Level JavaDoc;
26 import javax.jbi.messaging.MessagingException;
27 import javax.jbi.messaging.ExchangeStatus;
28 import javax.jbi.messaging.MessageExchange;
29
30
31 /**
32  * An instance of this class is used to send a message to
33  * NMR. Messages can be send either using the caller's thread
34  * or in a new thread.
35  *
36  * @author Binod PG
37  * @see OneWork
38  */

39 public class MessageSender extends OneWork {
40
41     /**
42      * Users of this class execute send() method to send
43      * the message to NMR. Note that execute() method will
44      * finally call doWork().
45      */

46     public void send() {
47         execute();
48     }
49
50     /**
51      * Actually send the message to NMR.
52      */

53     public void doWork() {
54         try {
55             if (logger.isLoggable(Level.FINEST)) {
56                 logger.log(Level.FINEST,
57                 "Sending the message " + getMessageExchange() + "to NMR");
58             }
59     
60             boolean sendSync = shouldSendSync(getMessageExchange());
61             if(sendSync) {
62                 getDeliveryChannel().sendSync(getMessageExchange());
63             } else {
64                 getDeliveryChannel().send(getMessageExchange());
65             }
66             
67             if (getMessageExchange().getStatus()== ExchangeStatus.ERROR)
68                 setException(getMessageExchange().getError());
69             
70             if (logger.isLoggable(Level.FINEST)) {
71                 logger.log(Level.FINEST,
72                 "Sent message " + getMessageExchange() + "to NMR");
73             }
74         } catch (MessagingException me) {
75             logger.log(Level.WARNING, "Error sending message" + me.getMessage());
76             setException(me);
77         }
78     }
79     
80     private boolean shouldSendSync(MessageExchange exchange) {
81         boolean sendSync = true;
82         
83         ExchangeStatus status = getMessageExchange().getStatus();
84
85         /**
86          * As per JBI 1.0 specification (sections #5.4.2.2, #5.4.2.3, 5.4.2.3, 5.4.2.5) :
87          * If the provider/consumer response is 'status', the exchange is complete.
88          */

89         if (ExchangeStatus.DONE.equals(status) || ExchangeStatus.ERROR.equals(status)) {
90             sendSync = false;
91         }
92
93     return sendSync;
94     }
95 }
96
Popular Tags