因为JMS是非同步的,所以在接收同一种Queue进行处理的时候,很容易发生资源争抢。如果我们把接收Queue的处理做成一个单线程,就可以避免这种问题。
我们利用Spring自带的ServerSessionMessageListenerContainer,稍微配置一下就可以了
<
bean
id
="listenerContainerQueue"
lazy-init
="true"
class
="org.springframework.jms.listener.serversession.ServerSessionMessageListenerContainer"
>
<
property
name
="sessionTransacted"
value
="true"
/>
<
property
name
="connectionFactory"
ref
="myConnectionFactory"
/>
<
property
name
="serverSessionFactory"
>
<
bean
class
="org.springframework.jms.listener.serversession.SimpleServerSessionFactory"
>
<
property
name
="taskExecutor"
>
<
bean
class
="org.springframework.core.task.SyncTaskExecutor"
/>
</
property
>
</
bean
>
</
property
>
<
property
name
="destinationName"
value
="A"
/>
<
property
name
="messageListener"
ref
="messageListener"
/>
<
property
name
="exceptionListener"
ref
="exceptionListener"
/>
</
bean
>
注:org.springframework.core.task.SyncTaskExecutor是一个单线程的连接池
我们利用Spring自带的ServerSessionMessageListenerContainer,稍微配置一下就可以了
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
注:org.springframework.core.task.SyncTaskExecutor是一个单线程的连接池