使用Weblogic JMS监控log4j的日志
配置CLASSPATH下的log4j.properties:
# This is the configuring for logging displayed in the Application Server;delete ,stdout log4j.rootCategory=INFO,R,A1 log4j.appender.A1=org.apache.log4j.net.JMSAppender log4j.appender.A1.providerURL=t3://localhost:7001 #log4j.appender.A1.userName=weblogic #log4j.appender.A1.password=weblogic log4j.appender.A1.topicConnectionFactoryBindingName=jms/JNDI_JMS_Connection_Factory log4j.appender.A1.topicBindingName=jms/JNDI_LOG_TOPIC log4j.appender.A1.initialContextFactoryName=weblogic.jndi.WLInitialContextFactory |
注意:
1,应该是消息主题(TOPIC),下面的配置和代码也应该为topic,而不是queue
2,Log4j的版本是log4j-
配置JMS Servers:
配置JMS Modules (JMS模块):
代码:
MessagingExampleBean.java:
package com.sunrise.psmis.ejb;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;
import org.apache.log4j.spi.LoggingEvent;
publicclass MessagingExampleBean implements javax.ejb.MessageDrivenBean,
javax.jms.MessageListener {
private javax.ejb.MessageDrivenContext messageContext = null;
publicvoid setMessageDrivenContext(
javax.ejb.MessageDrivenContext messageContext)
throws javax.ejb.EJBException {
System.out.println("设置上下文");
this.messageContext = messageContext;
}
publicvoid ejbCreate() {
System.out.println("创建消息Bean");
}
publicvoid ejbRemove() {
messageContext = null;
}
public MessagingExampleBean() {
}
publicvoid onMessage(javax.jms.Message message) {
String msgText="";
if (message instanceof TextMessage) {
try{
msgText = ((TextMessage) message).getText();
}catch(JMSException e){
e.printStackTrace();
}
} elseif (message instanceof ObjectMessage) {
try{
Object obj=((ObjectMessage) message).getObject();
if(obj instanceof LoggingEvent){
LoggingEvent event=(LoggingEvent)obj;
System.out.println(event.getRenderedMessage());
}
}catch(JMSException e){
e.printStackTrace();
}
}
System.out.println("返回消息:"+msgText);
}
}
|
ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>MessagingExample</display-name>
<enterprise-beans>
<message-driven>
<display-name>MessagingExampleBean</display-name>
<ejb-name>MessagingExampleBean</ejb-name>
<ejb-class>com.sunrise.psmis.ejb.MessagingExampleBean</ejb-class>
<transaction-type>Bean</transaction-type>
<message-destination-type>
javax.jms.Topic
</message-destination-type>
</message-driven>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>MessagingExampleBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
|
weblogic-ejb-jar.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC "-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN" "http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd">
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>MessagingExampleBean</ejb-name>
<message-driven-descriptor>
<pool>
<max-beans-in-free-pool>5</max-beans-in-free-pool>
<initial-beans-in-free-pool>
5
</initial-beans-in-free-pool>
</pool>
<destination-jndi-name>jms/JNDI_LOG_TOPIC</destination-jndi-name>
<initial-context-factory>
weblogic.jndi.WLInitialContextFactory
</initial-context-factory>
<connection-factory-jndi-name>
jms/JNDI_JMS_Connection_Factory
</connection-factory-jndi-name>
<jms-polling-interval-seconds>
20
</jms-polling-interval-seconds>
</message-driven-descriptor>
<transaction-descriptor>
<trans-timeout-seconds>3600</trans-timeout-seconds>
</transaction-descriptor>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
|
参考: