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 org.springframework.aop.TargetSource;
21  import org.springframework.beans.factory.InitializingBean;
22  
23  import javax.jms.Message;
24  import javax.jms.MessageListener;
25  
26  /***
27   * Represents a pool of MessageListener instances using Spring's
28   * TargetSource for the underlying pool.
29   *
30   * @version $Revision: 1.1.1.1 $
31   */
32  public class TargetSourceMessageListener implements MessageListener, InitializingBean {
33      private TargetSource targetSource;
34  
35      public void onMessage(Message message) {
36          MessageListener delegate = null;
37          try {
38              delegate = (MessageListener) targetSource.getTarget();
39          }
40          catch (Exception e) {
41              handleException(e);
42          }
43          try {
44              delegate.onMessage(message);
45          }
46          finally {
47              try {
48                  targetSource.releaseTarget(delegate);
49              }
50              catch (Exception e) {
51                  handleException(e);
52              }
53          }
54      }
55  
56      public void afterPropertiesSet() throws Exception {
57          if (targetSource == null) {
58              throw new IllegalArgumentException("targetSource must be set");
59          }
60      }
61  
62      public TargetSource getTargetSource() {
63          return targetSource;
64      }
65  
66      public void setTargetSource(TargetSource targetSource) {
67          this.targetSource = targetSource;
68      }
69  
70      protected void handleException(Exception e) {
71          throw new RuntimeException(e);
72      }
73  
74  }