View Javadoc

1   /*
2    * Copyright 2005-2010 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      @Override
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  }