【发布时间】:2018-08-16 15:12:42
【问题描述】:
我正在 Wildfly 10.1 Final 上运行 Java EE 项目。
我想制作一个自定义 Log4j 附加程序,用于记录到 JMS 队列。 我已经在我的 Java EE 应用程序中创建了一个 Message Driven Bean,它接收来自队列的日志消息。
我通过添加此注释创建了我的 MDB:
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination",
propertyValue = "queue/MyQueue")
})
这个 bean 还实现了 MessageListener 接口,并覆盖了它的 onMessage() 方法,我在该方法中定义了日志消息到达时所需的行为。它看起来像这样:
public class myBean implements MessageListener {
@Override
public void onMessage(Message msg) {
if(msg instanceof TextMessage){
TextMessage txtMess = (TextMessage)msg;
String context = (String)txtMess.getText();
processLogData(context);
}
}
我还需要将 JMS 添加到 Wildfly 的 standalone.xml,方法是从 standalone-full.xml 复制所有与 JMS 相关的行。我还需要将我的队列添加到 standalone.xml,方法是将这一行添加到 :
<jms-queue name="SequenceQueue" entries="java:/jms/queue/MyQueue"/>
现在,我想使用 apache 提供的JMSQueueAppender 类,将日志消息发送到 MyQueue。
我不确定,我应该怎么做。我应该把JMSQueueApender 类放在哪里?我应该为 queueConnectionFactoryBindingName 和 queueBindingName 以及 initialContextFactory 和 providerURL 提供什么值?
我读到了这个SO 线程,我应该把log4j.properties 文件放在我的EAR/META-INF 中。我还在this SO 线程中阅读了 log4j.properties 的外观。
不过,我不确定需要向 JMSQueueApender 类提供哪些值,以及应该将其放在哪里(如何初始化)。
由于我的项目在 Application Server(Java EE 环境)中运行,并且我的队列是该容器的一部分,我可以编写一个更简单的 JMSQueuApender 类吗?像这样的:
@Singleton
@Startup
public class JMSQueueAppender extends AppenderSkeleton {
private Logger log = LoggerFactory.getLogger(LogSomethingBean.class);
@Inject
private JMSContext context;
@Resource(mappedName = "java:/jms/queue/MyQueue")
private Queue queue;
public void append(LoggingEvent event) {
try {
TextMessage msg = queueSession.createTextMessage();
String str = (String)event.getMessage();
msg.setText(str);
context.createProducer().send(queue, msg);
} catch (JMSException e) {
log.error("An error has occured while trying to send a msg to queue", e);
}
}
}
编辑:是否有可能为整个 Wildfly 实例定义一个 log4j.properties 文件,以便在其上运行的每个项目都读取该配置?
【问题讨论】:
标签: java jakarta-ee log4j jms wildfly