View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
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.springframework.ws.transport.support;
18  
19  import org.springframework.beans.factory.BeanNameAware;
20  import org.springframework.core.task.SimpleAsyncTaskExecutor;
21  import org.springframework.core.task.TaskExecutor;
22  import org.springframework.scheduling.commonj.WorkManagerTaskExecutor;
23  import org.springframework.util.ClassUtils;
24  
25  /**
26   * Abstract base class for asynchronous standalone, server-side transport objects. Contains a Spring {@link
27   * TaskExecutor}, and various lifecycle callbacks.
28   *
29   * @author Arjen Poutsma
30   */
31  public abstract class AbstractAsyncStandaloneMessageReceiver extends AbstractStandaloneMessageReceiver
32          implements BeanNameAware {
33  
34      /** Default thread name prefix. */
35      public final String DEFAULT_THREAD_NAME_PREFIX = ClassUtils.getShortName(getClass()) + "-";
36  
37      private TaskExecutor taskExecutor;
38  
39      private String beanName;
40  
41      /**
42       * Set the Spring {@link TaskExecutor} to use for running the listener threads. Default is {@link
43       * SimpleAsyncTaskExecutor}, starting up a number of new threads.
44       * <p/>
45       * Specify an alternative task executor for integration with an existing thread pool, such as the {@link
46       * WorkManagerTaskExecutor} to integrate with WebSphere or WebLogic.
47       */
48      public void setTaskExecutor(TaskExecutor taskExecutor) {
49          this.taskExecutor = taskExecutor;
50      }
51  
52      public void setBeanName(String beanName) {
53          this.beanName = beanName;
54      }
55  
56      public void afterPropertiesSet() throws Exception {
57          if (taskExecutor == null) {
58              taskExecutor = createDefaultTaskExecutor();
59          }
60          super.afterPropertiesSet();
61      }
62  
63      /**
64       * Create a default TaskExecutor. Called if no explicit TaskExecutor has been specified.
65       * <p/>
66       * The default implementation builds a {@link org.springframework.core.task.SimpleAsyncTaskExecutor} with the
67       * specified bean name (or the class name, if no bean name specified) as thread name prefix.
68       *
69       * @see org.springframework.core.task.SimpleAsyncTaskExecutor#SimpleAsyncTaskExecutor(String)
70       */
71      protected TaskExecutor createDefaultTaskExecutor() {
72          String threadNamePrefix = beanName != null ? beanName + "-" : DEFAULT_THREAD_NAME_PREFIX;
73          return new SimpleAsyncTaskExecutor(threadNamePrefix);
74      }
75  
76      /**
77       * Executes the given {@link Runnable} via this receiver's {@link TaskExecutor}.
78       *
79       * @see #setTaskExecutor(TaskExecutor)
80       */
81      protected void execute(Runnable runnable) {
82          taskExecutor.execute(runnable);
83      }
84  }