KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > listener > serversession > SimpleServerSessionFactory


1 /*
2  * Copyright 2002-2006 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.jms.listener.serversession;
18
19 import javax.jms.JMSException JavaDoc;
20 import javax.jms.ServerSession JavaDoc;
21 import javax.jms.Session JavaDoc;
22
23 import org.springframework.core.task.SimpleAsyncTaskExecutor;
24 import org.springframework.core.task.TaskExecutor;
25 import org.springframework.jms.support.JmsUtils;
26 import org.springframework.util.Assert;
27 import org.springframework.util.ClassUtils;
28
29 /**
30  * The simplest possible implementation of the ServerSessionFactory SPI:
31  * creating a new ServerSession with a new JMS Session every time.
32  * This is the default used by ServerSessionMessageListenerContainer.
33  *
34  * <p>The execution of a ServerSession (and its MessageListener) gets delegated
35  * to a TaskExecutor. By default, a SimpleAsyncTaskExecutor will be used,
36  * creating a new Thread for every execution attempt. Alternatives are a
37  * TimerTaskExecutor, sharing a single Thread for the execution of all
38  * ServerSessions, or a TaskExecutor implementation backed by a thread pool.
39  *
40  * <p>To reuse JMS Sessions and/or to limit the number of concurrent
41  * ServerSession executions, consider using a pooling ServerSessionFactory:
42  * for example, CommonsPoolServerSessionFactory.
43  *
44  * @author Juergen Hoeller
45  * @since 2.0
46  * @see org.springframework.core.task.TaskExecutor
47  * @see org.springframework.core.task.SimpleAsyncTaskExecutor
48  * @see org.springframework.scheduling.timer.TimerTaskExecutor
49  * @see CommonsPoolServerSessionFactory
50  * @see ServerSessionMessageListenerContainer
51  */

52 public class SimpleServerSessionFactory implements ServerSessionFactory {
53
54     /**
55      * Default thread name prefix: "SimpleServerSessionFactory-".
56      */

57     public static final String JavaDoc DEFAULT_THREAD_NAME_PREFIX =
58             ClassUtils.getShortName(SimpleServerSessionFactory.class) + "-";
59
60
61     private TaskExecutor taskExecutor = new SimpleAsyncTaskExecutor(DEFAULT_THREAD_NAME_PREFIX);
62
63
64     /**
65      * Specify the TaskExecutor to use for executing ServerSessions
66      * (and consequently, the underlying MessageListener).
67      * <p>Default is a SimpleAsyncTaskExecutor, creating a new Thread for
68      * every execution attempt. Alternatives are a TimerTaskExecutor,
69      * sharing a single Thread for the execution of all ServerSessions,
70      * or a TaskExecutor implementation backed by a thread pool.
71      * @see org.springframework.core.task.SimpleAsyncTaskExecutor
72      * @see org.springframework.scheduling.timer.TimerTaskExecutor
73      */

74     public void setTaskExecutor(TaskExecutor taskExecutor) {
75         Assert.notNull(taskExecutor, "taskExecutor is required");
76         this.taskExecutor = taskExecutor;
77     }
78
79     /**
80      * Return the TaskExecutor to use for executing ServerSessions.
81      */

82     protected TaskExecutor getTaskExecutor() {
83         return taskExecutor;
84     }
85
86
87     /**
88      * Creates a new SimpleServerSession with a new JMS Session
89      * for every call.
90      */

91     public ServerSession JavaDoc getServerSession(ListenerSessionManager sessionManager) throws JMSException JavaDoc {
92         return new SimpleServerSession(sessionManager);
93     }
94
95     /**
96      * This implementation is empty, as there is no state held for
97      * each ListenerSessionManager.
98      */

99     public void close(ListenerSessionManager sessionManager) {
100     }
101
102
103     /**
104      * ServerSession implementation that simply creates a new
105      * JMS Session and executes it via the specified TaskExecutor.
106      */

107     private class SimpleServerSession implements ServerSession JavaDoc {
108
109         private final ListenerSessionManager sessionManager;
110
111         private final Session JavaDoc session;
112
113         public SimpleServerSession(ListenerSessionManager sessionManager) throws JMSException JavaDoc {
114             this.sessionManager = sessionManager;
115             this.session = sessionManager.createListenerSession();
116         }
117
118         public Session JavaDoc getSession() {
119             return session;
120         }
121
122         public void start() {
123             getTaskExecutor().execute(new Runnable JavaDoc() {
124                 public void run() {
125                     try {
126                         sessionManager.executeListenerSession(session);
127                     }
128                     finally {
129                         JmsUtils.closeSession(session);
130                     }
131                 }
132             });
133         }
134     }
135
136 }
137
Popular Tags