View Javadoc

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