KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > jms > ReceiveTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.jms;
18
19 import javax.jms.Destination JavaDoc;
20 import javax.jms.JMSException JavaDoc;
21 import javax.jms.Message JavaDoc;
22 import javax.servlet.jsp.JspException JavaDoc;
23
24 /** Receives a JMS message.
25   *
26   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
27   * @version $Revision: 1.2 $
28   */

29 public class ReceiveTag extends MessageOperationTag {
30
31     private static final long DEFAULT_TIMEOUT = -1L;
32     
33     private long timeout = DEFAULT_TIMEOUT;
34     private String JavaDoc var;
35     
36     public ReceiveTag() {
37     }
38     
39     // Tag interface
40
//-------------------------------------------------------------------------
41
public int doStartTag() throws JspException JavaDoc {
42         return EVAL_BODY_INCLUDE;
43     }
44     
45     public int doEndTag() throws JspException JavaDoc {
46         try {
47             Destination JavaDoc destination = getDestination();
48             if ( destination == null ) {
49                 throw new JspException JavaDoc( "No destination specified. Either specify a 'destination' attribute or use a nested <jms:destination> tag" );
50             }
51             Message JavaDoc message = null;
52             if ( timeout > 0 ) {
53                 message = getConnection().receive( destination, timeout );
54             }
55             else if ( timeout == 0 ) {
56                 message = getConnection().receiveNoWait( destination );
57             }
58             else {
59                 message = getConnection().receive( destination );
60             }
61             onMessage( message );
62         }
63         catch (JMSException JavaDoc e) {
64             throw new JspException JavaDoc( "Failed to receive JMS message: " + e, e );
65         }
66         return EVAL_PAGE;
67     }
68     
69     public void release() {
70         super.release();
71         timeout = DEFAULT_TIMEOUT;
72         var = null;
73     }
74     
75     
76     // Properties
77
//-------------------------------------------------------------------------
78
public String JavaDoc getVar() {
79         return var;
80     }
81     
82     public void setVar(String JavaDoc var) {
83         this.var = var;
84     }
85     
86     public long getTimeout() {
87         return timeout;
88     }
89     
90     public void setTimeout(long timeout) {
91         this.timeout = timeout;
92     }
93     
94     // Implementation methods
95
//-------------------------------------------------------------------------
96

97     /** Processes the incoming message */
98     protected void onMessage( Message JavaDoc message ) {
99         if ( message != null ) {
100             pageContext.setAttribute( var, message );
101         }
102         else {
103             pageContext.removeAttribute( var );
104         }
105     }
106 }
107
Popular Tags