KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > jbi > serviceengine > work > OneWork


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
24 package com.sun.enterprise.jbi.serviceengine.work;
25
26 import java.util.logging.Logger JavaDoc;
27 import javax.jbi.messaging.MessageExchange;
28 import javax.jbi.messaging.DeliveryChannel;
29 import com.sun.enterprise.jbi.serviceengine.ServiceEngineException;
30 import com.sun.corba.ee.spi.orbutil.threadpool.Work;
31 import com.sun.enterprise.jbi.serviceengine.core.JavaEEServiceEngineContext;
32 import com.sun.logging.LogDomains;
33
34 /**
35  * Represents one piece of work that will be submitted to the workqueue.
36  *
37  * @author Binod P.G
38  */

39 public abstract class OneWork implements Work {
40
41     private long nqTime;
42     protected static final Logger JavaDoc logger =
43         LogDomains.getLogger(LogDomains.SERVER_LOGGER);
44     private MessageExchange me = null;
45     private DeliveryChannel channel = null;
46     private boolean useCurrentThread = true;
47     private JavaEEServiceEngineContext seContext;
48     private WorkManager wm = null;
49     private ServiceEngineException exception = null;
50
51
52     /**
53      * Initializes the work. Save a local copy of delivery channel
54      * and work manager.
55      */

56     public OneWork() {
57         this.channel = JavaEEServiceEngineContext.getInstance().getDeliveryChannel();
58         this.wm = JavaEEServiceEngineContext.getInstance().getWorkManager();
59     }
60
61     /**
62      * This method is executed by thread pool as the basic work operation.
63      */

64     public abstract void doWork();
65     
66     /**
67      * Time at which this work is enqueued.
68      *
69      * @param tme Time in milliseconds.
70      */

71     public void setEnqueueTime(long tme) {
72         this.nqTime = tme;
73     }
74
75     /**
76      * Retrieves the time at which this work is enqueued
77      *
78      * @return Time in milliseconds.
79      */

80     public long getEnqueueTime() {
81         return nqTime;
82     }
83
84     /**
85      * Set the MEP associated with this piece of work.
86      */

87     public void setMessageExchange(MessageExchange me) {
88         this.me = me;
89     }
90
91     /**
92      * Retrieves the MEP.
93      */

94     public MessageExchange getMessageExchange() {
95         return me;
96     }
97
98     /**
99      * Set a boolean indicating whether current thread should be used
100      * for execution of this work.
101      *
102      * @param flag If set to true, then the current callng thread will
103      * be used for executing the thread. If set to false, the work will
104      * be submitted to the Queue of the thread pool.
105      */

106     public void setUseCurrentThread(boolean flag) {
107         this.useCurrentThread = flag;
108     }
109
110     /**
111      * Retrieves the flag indicating whether the current thread should
112      * be used for work execution or not.
113      */

114     public boolean getUseCurrentThread() {
115         return this.useCurrentThread;
116     }
117
118     /**
119      * Retrieves the work manager instance.
120      */

121     public WorkManager getWorkManager() {
122         return this.wm;
123     }
124
125     /**
126      * Retrieves the delivery channel object.
127      */

128     public DeliveryChannel getDeliveryChannel() {
129         return this.channel;
130     }
131
132     /**
133      * Get the exception, if any produced while executng
134      * this work.
135      */

136     public ServiceEngineException getException() {
137         return this.exception;
138     }
139
140     /**
141      * Convenience method to set the execption object
142      * produced while executing the work.
143      */

144     public void setException(Throwable JavaDoc t) {
145         this.exception = new ServiceEngineException(t);
146     }
147
148     /**
149      * Execute the work. If current thread should be used,
150      * doWork is called directly. Otherwise, work is submitted
151      * to the thread pool.
152      */

153     protected void execute() {
154         if (getUseCurrentThread()) {
155             doWork();
156         } else {
157             getWorkManager().submitWork(this);
158         }
159     }
160
161     /**
162      * Retrieves the name of the work.
163      *
164      * @return Name of the work.
165      */

166     public String JavaDoc getName() {
167         return "One JBI Work";
168     }
169 }
170
Popular Tags