spring2.0.8和activemq5.2 topic方式,消息被重复消费的问题找到原因

找了好几个月,终于找到了问题的所在。

因为以前在后台的java类中重复加载了spring配置文件,导致topic的消费者被重复增加,所以一个消息被消费了两次。

 

package com.work.core.spring;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 通过静态代码来获取spring的context,第一次调用的使用运行。
 *
 * @author wangmingjie
 *
 */
public class MyBeanUtil {
//因为下面的代码导致了activemqtopic订阅方式,每次都被消费了两次。
 
//在非web环境中获取bean的方法。
// private static ApplicationContext context = new ClassPathXmlApplicationContext(
//   "applicationContext*.xml");

 
 private static ApplicationContext context ;

 public static ApplicationContext getContext() {
  return context;
 }

 /**
  * 通过权限管理QxglInitServlet来设置context
  * @param context
  */
 public static void setContext(ApplicationContext context) {
  MyBeanUtil.context = context;
 }

 /**
  * 在应用程序中,获取到spring的beans对象。<br>
  *  首先在QxglInitServlet中设置了context。
  * @param beanName
  * @return
  */
 public static Object getBean(String beanName) {
  return context.getBean(beanName);
 }

 /**
  * 在web环境中,通过WebApplicationContext来获取spring的bean。
  * @param request
  * @param beanName
  * @return
  */
 public static Object getBean(HttpServletRequest request, String beanName) {
  WebApplicationContext webApplicationContext = WebApplicationContextUtils
    .getWebApplicationContext(request.getSession()
      .getServletContext());
  return webApplicationContext.getBean(beanName);
 }
 
 /**
  * 在web环境中,通过WebApplicationContext来获取spring的bean。
  * @param context
  * @param beanName
  * @return
  */
 public static Object getBean(ServletContext context, String beanName) {
  WebApplicationContext webApplicationContext = WebApplicationContextUtils
    .getWebApplicationContext(context);
  return webApplicationContext.getBean(beanName);
 } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值