View Javadoc

1   /***
2    * 
3    * Copyright 2005 LogicBlaze, Inc.
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.jencks;
19  
20  import javax.jms.JMSException;
21  import javax.jms.Message;
22  import javax.jms.MessageListener;
23  import javax.resource.ResourceException;
24  import javax.resource.spi.endpoint.MessageEndpoint;
25  import java.lang.reflect.Method;
26  
27  /***
28   * An endpoint which uses a JMS {@link javax.jms.Message#acknowledge()} to
29   * acknowledge that a message is delivered.
30   *
31   * @version $Revision: 1.1.1.1 $
32   */
33  public class AcknowledgeEndpoint implements MessageEndpoint, MessageListener {
34  
35      private MessageListener messageListener;
36      private Message message;
37  
38      public AcknowledgeEndpoint(MessageListener messageListener) {
39          this.messageListener = messageListener;
40      }
41  
42      public void beforeDelivery(Method method) throws NoSuchMethodException, ResourceException {
43      }
44  
45      public void afterDelivery() throws ResourceException {
46          if (message == null) {
47              throw new ResourceException("No message has been delivered yet");
48          }
49          try {
50              message.acknowledge();
51          }
52          catch (JMSException e) {
53              throw new ResourceException(e);
54          }
55          finally {
56              message = null;
57          }
58      }
59  
60      public void release() {
61      }
62  
63      public void onMessage(Message message) {
64          messageListener.onMessage(message);
65          this.message = message;
66      }
67  }