spring单例注入init

Java类

import java.util.HashMap;
import java.util.Map;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import net.sf.json.JSONObject;
import org.slf4j.MDC;
import com.yk.platform.framework.message.MessageFactory;
import com.yk.platform.logger.PLogger;


public class LogMessageHandler implements MessageListener{

	private final String USER_KEY = "userid"; 	//MDC的key值,用于生成不同的用户日志
	private final String DepartName = "departName";	//MDC的Key值,部门文件夹下是分用户的日志文件
    private String topicId;	//从配置中读取,默认值为log
    private PLogger log;
    private String url;		//消息中间件url,从配置文件中注入
    
    
	public PLogger getLog() {
		return log;
	}

	public void setLog(PLogger log) {
		this.log = log;
	}
	public void setTopicId(final String topicId){
		this.topicId = topicId;
	}

	public void setUrl(final String url){
		this.url = url;
	}
	/*
	 * 在项目启动时,进行初始化。
	 */
	public void init(){
		Map map = new HashMap();
		map.put("url", url);
		map.put("clientId", topicId);
		map.put("destination", topicId);
		map.put("topicId", topicId);
		try {
			//注册消息中间件监听
			MessageFactory.registerMessageListener(map, this);
			/*String string = flag?"打开监听成功!":"打开监听失败!";
			System.out.println();*/
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public LogMessageHandler() {
	}
	  
	@Override
	public void onMessage(Message message) {
		try {
			String content[] = null;
			String logType = "";
			/*
			 * 接收来自消息中间件的日志信息
			 */
			if(message instanceof TextMessage){
				TextMessage textMessage = (TextMessage)message;
				JSONObject jso = JSONObject .fromObject(textMessage.getText());
				//日志内容,格式:用户ID+用户所属组ID+日志类型+日志内容
				String temp = (String)jso.get("content");
				if(temp!=null){
						content = ((String)temp).split(";");
				}
				if(content!=null&&content.length>3){
					/*
					 * MDC中put相关用户信息,用于生成按组织和用户生成客户端日志
					 */
					MDC.put(USER_KEY, "clientLog"+content[0]);
					MDC.put(DepartName, "clientLog"+content[1]);
					logType = content[2];
					if(logType.equals("error")){
						log.error(content[3]);
					}else if(logType.equals("warn")){
						log.warn(content[3]);
					}else if(logType.equals("debug")){
						log.debug(content[3]);
					}else{
						log.info(content[3]);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			MDC.remove(USER_KEY);
			MDC.remove(DepartName);
		}
	}
	
}


xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="   
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd   
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
     
	<bean id = "log" class= "com.yk.platform.logger.impl.ServerPLoggerImpl" />

	<!-- 必须启动消息中间件,否则在服务启动时,会抛错 -->
	<bean id="logMessagehandler" class="com.yk.platform.framework.message.handler.LogMessageHandler" scope="singleton"  init-method="init">
		<property name="log" ref="log"></property>
		<property name="topicId" value="log"></property>
		<property name="url" value="tcp://localhost:61616"></property>
	</bean>
</beans>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值