您的位置:首页 > 编程语言 > Java开发

spring单例注入init

2016-01-11 16:33 639 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: