1 16 17 package org.apache.taglibs.jms; 18 19 import javax.jms.Destination ; 20 import javax.jms.JMSException ; 21 import javax.jms.Message ; 22 import javax.servlet.jsp.JspException ; 23 24 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 var; 35 36 public ReceiveTag() { 37 } 38 39 public int doStartTag() throws JspException { 42 return EVAL_BODY_INCLUDE; 43 } 44 45 public int doEndTag() throws JspException { 46 try { 47 Destination destination = getDestination(); 48 if ( destination == null ) { 49 throw new JspException ( "No destination specified. Either specify a 'destination' attribute or use a nested <jms:destination> tag" ); 50 } 51 Message 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 e) { 64 throw new JspException ( "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 public String getVar() { 79 return var; 80 } 81 82 public void setVar(String 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 97 98 protected void onMessage( Message message ) { 99 if ( message != null ) { 100 pageContext.setAttribute( var, message ); 101 } 102 else { 103 pageContext.removeAttribute( var ); 104 } 105 } 106 } 107 | Popular Tags |