http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderIT.java ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderIT.java b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderIT.java new file mode 100644 index 0000000..9ed98fa --- /dev/null +++ b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderIT.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.mom.jms.appender; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageListener; +import javax.jms.ObjectMessage; + +import org.apache.activemq.jndi.ActiveMQInitialContextFactory; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.core.layout.SerializedLayout; +import org.apache.logging.log4j.message.SimpleMessage; +import org.apache.logging.log4j.mom.jms.manager.JmsManager; +import org.apache.logging.log4j.mom.jms.manager.JndiManager; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Integration test for JmsAppender using an embedded ActiveMQ broker. + */ +public class JmsAppenderIT { + + private static JmsManager jmsManager; + + private JmsAppender appender; + + @BeforeClass + public static void setUpClass() { + final Properties additional = new Properties(); + additional.setProperty("queue.TestQueue", "TestQueue"); + JndiManager jndiManager = JndiManager.getJndiManager(ActiveMQInitialContextFactory.class.getName(), + "vm://localhost?broker.persistent=false", null, null, null, additional); + jmsManager = JmsManager.getJmsManager("JmsManager", jndiManager, "ConnectionFactory", "TestQueue", null, null); + } + + @AfterClass + public static void tearDownClass() { + jmsManager.release(); + } + + @Before + public void setUp() throws Exception { + appender = new JmsAppender("JmsAppender", null, SerializedLayout.createLayout(), true, jmsManager); + appender.start(); + } + + @Test + public void testLogToQueue() throws Exception { + final int messageCount = 100; + final MessageConsumer messageConsumer = jmsManager.createMessageConsumer(); + final JmsQueueConsumer consumer = new JmsQueueConsumer(messageCount); + messageConsumer.setMessageListener(consumer); + final String messageText = "Hello, World!"; + final String loggerName = this.getClass().getName(); + for (int i = 0; i < messageCount; i++) { + final LogEvent event = Log4jLogEvent.createEvent(loggerName, null, loggerName, Level.ERROR, + new SimpleMessage(messageText), null, null, null, null, Thread.currentThread().getName(), null, + System.currentTimeMillis()); + appender.append(event); + } + consumer.awaitAndAssertAllMessagesConsumed(); + } + + private static class JmsQueueConsumer implements MessageListener { + + private final int messageCount; + private final CountDownLatch countDownLatch; + private final Collection<LogEvent> events; + + private JmsQueueConsumer(final int messageCount) { + this.messageCount = messageCount; + this.countDownLatch = new CountDownLatch(messageCount); + this.events = new ArrayList<LogEvent>(messageCount); + } + + @Override + public void onMessage(Message message) { + try { + consume((ObjectMessage) message); + } catch (JMSException e) { + e.printStackTrace(); + } + } + + private void consume(ObjectMessage message) throws JMSException { + try { + final LogEvent event = (LogEvent) message.getObject(); + events.add(event); + } finally { + countDownLatch.countDown(); + } + } + + public void awaitAndAssertAllMessagesConsumed() throws InterruptedException { + countDownLatch.await(5, TimeUnit.SECONDS); + assertEquals(messageCount, events.size()); + } + } +}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderTest.java ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderTest.java b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderTest.java new file mode 100644 index 0000000..8e54681 --- /dev/null +++ b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderTest.java @@ -0,0 +1,79 @@ +package org.apache.logging.log4j.mom.jms.appender; + +import javax.jms.Message; +import javax.jms.TextMessage; +import javax.naming.Context; +import javax.naming.InitialContext; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.core.util.Closer; +import org.apache.logging.log4j.junit.InitialLoggerContext; +import org.apache.logging.log4j.message.SimpleMessage; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.mockejb.jms.MockQueue; +import org.mockejb.jms.QueueConnectionFactoryImpl; +import org.mockejb.jndi.MockContextFactory; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.*; + +public class JmsAppenderTest { + + private static final String CONNECTION_FACTORY_NAME = "jms/activemq"; + private static final String DESTINATION_NAME = "jms/destination"; + private static final String LOG_MESSAGE = "Hello, world!"; + + private static Context context; + + private JmsAppender appender; + private static MockQueue queue; + + @BeforeClass + public static void setUpClass() throws Exception { + MockContextFactory.setAsInitial(); + context = new InitialContext(); + context.rebind(CONNECTION_FACTORY_NAME, new QueueConnectionFactoryImpl()); + queue = new MockQueue(DESTINATION_NAME); + context.rebind(DESTINATION_NAME, queue); + } + + @AfterClass + public static void tearDownClass() throws Exception { + Closer.closeSilently(context); + } + + @Rule + public InitialLoggerContext ctx = new InitialLoggerContext("JmsAppenderTest.xml"); + + @Before + public void setUp() throws Exception { + appender = (JmsAppender) ctx.getAppender("JmsQueueAppender"); + assertEquals(0, queue.size()); + } + + @Test + public void testAppendToQueue() throws Exception { + final String loggerName = this.getClass().getName(); + final long now = System.currentTimeMillis(); + final LogEvent event = createLogEvent(loggerName, now); + appender.append(event); + assertEquals(1, queue.size()); + final Message message = queue.getMessageAt(0); + assertNotNull(message); + assertThat(message, instanceOf(TextMessage.class)); + final TextMessage textMessage = (TextMessage) message; + assertEquals(LOG_MESSAGE, textMessage.getText()); + } + + private static Log4jLogEvent createLogEvent(String loggerName, long now) { + return Log4jLogEvent.createEvent(loggerName, null, loggerName, Level.INFO, + new SimpleMessage(LOG_MESSAGE), null, null, null, null, Thread.currentThread().getName(), null, now); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueAppenderTest.java ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueAppenderTest.java b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueAppenderTest.java new file mode 100644 index 0000000..f05a9fc --- /dev/null +++ b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueAppenderTest.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.mom.jms.appender; + +import java.util.Map; + +import javax.naming.Context; +import javax.naming.InitialContext; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.mom.jms.receiver.AbstractJmsReceiver; +import org.apache.logging.log4j.mom.jms.receiver.JmsQueueReceiver; +import org.apache.logging.log4j.status.StatusConsoleListener; +import org.apache.logging.log4j.status.StatusLogger; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockejb.jms.MockQueue; +import org.mockejb.jms.QueueConnectionFactoryImpl; +import org.mockejb.jndi.MockContextFactory; + +import static org.junit.Assert.*; + +/** + * + */ +public class JmsQueueAppenderTest { + + private static final String FACTORY_NAME = "TestQueueConnectionFactory"; + private static final String QUEUE_NAME = "TestQueue"; + + private static Context context; + private static AbstractJmsReceiver receiver; + + private static final String CONFIG = "log4j-jmsqueue.xml"; + + LoggerContext ctx = (LoggerContext) LogManager.getContext(); + Logger root = ctx.getLogger("JmsQueueTest"); + + @BeforeClass + public static void setupClass() throws Exception { + // MockContextFactory becomes the primary JNDI provider + final StatusConsoleListener listener = new StatusConsoleListener(Level.ERROR); + StatusLogger.getLogger().registerListener(listener); + MockContextFactory.setAsInitial(); + context = new InitialContext(); + context.rebind(FACTORY_NAME, new QueueConnectionFactoryImpl()); + context.rebind(QUEUE_NAME, new MockQueue(QUEUE_NAME)); + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); + receiver = new JmsQueueReceiver(FACTORY_NAME, QUEUE_NAME, null, null); + } + + @AfterClass + public static void cleanupClass() { + StatusLogger.getLogger().reset(); + } + + @Test + public void testConfiguration() throws Exception { + final LoggerContext ctx = (LoggerContext) LogManager.getContext(); + final Configuration config = ctx.getConfiguration(); + final Map<String, Appender> appenders = config.getAppenders(); + assertTrue(appenders.containsKey("JMSQueue")); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueFailoverTest.java ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueFailoverTest.java b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueFailoverTest.java new file mode 100644 index 0000000..25c3068 --- /dev/null +++ b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueFailoverTest.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.mom.jms.appender; + +import java.util.List; + +import javax.naming.Context; +import javax.naming.InitialContext; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.ThreadContext; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.mom.jms.receiver.AbstractJmsReceiver; +import org.apache.logging.log4j.mom.jms.receiver.JmsQueueReceiver; +import org.apache.logging.log4j.status.StatusConsoleListener; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockejb.jms.MockQueue; +import org.mockejb.jms.QueueConnectionFactoryImpl; +import org.mockejb.jndi.MockContextFactory; + +import static org.junit.Assert.*; + +/** + * + */ +public class JmsQueueFailoverTest { + + private static final String FACTORY_NAME = "QueueConnectionFactory"; + private static final String QUEUE_NAME = "Log4j2Queue"; + + private static Context context; + private static AbstractJmsReceiver receiver; + + private static final String CONFIG = "log4j-jmsqueue-failover.xml"; + + private static Configuration config; + private static ListAppender listAppender; + private static LoggerContext ctx; + + @BeforeClass + public static void setupClass() throws Exception { + setupQueue(); + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); + ctx = (LoggerContext) LogManager.getContext(false); + } + + @AfterClass + public static void cleanupClass() { + System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + ctx.reconfigure(); + StatusLogger.getLogger().reset(); + } + + @Before + public void before() { + config = ctx.getConfiguration(); + listAppender = (ListAppender) config.getAppender("List"); + assertNotNull("No Appender", listAppender); + listAppender.clear(); + ThreadContext.clearMap(); + } + + private static void setupQueue() throws Exception { + // MockContextFactory becomes the primary JNDI provider + final StatusConsoleListener listener = new StatusConsoleListener(Level.ERROR); + StatusLogger.getLogger().registerListener(listener); + MockContextFactory.setAsInitial(); + context = new InitialContext(); + context.rebind(FACTORY_NAME, new QueueConnectionFactoryImpl()); + //context.rebind(QUEUE_NAME, new MockQueue(QUEUE_NAME)); + //System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); + //receiver = new JmsQueueReceiver(FACTORY_NAME, QUEUE_NAME, null, null); + } + + @Test + public void testFailover() throws Exception { + ThreadContext.put("appender", "Failover"); + final Logger logger = LogManager.getLogger(JmsQueueFailoverTest.class); + logger.debug("Test Message"); + final List<LogEvent> events = listAppender.getEvents(); + assertNotNull("No events returned", events); + assertTrue("No events returned", events.size() > 0); + assertTrue("Incorrect event", "Test Message".equals(events.get(0).getMessage().getFormattedMessage())); + } + + @Test + public void testReconnect() throws Exception { + context.rebind(QUEUE_NAME, new MockQueue(QUEUE_NAME)); + receiver = new JmsQueueReceiver(FACTORY_NAME, QUEUE_NAME, null, null); + ThreadContext.put("appender", "Failover"); + final Logger logger = LogManager.getLogger(JmsQueueFailoverTest.class); + logger.debug("Test Message"); + final List<LogEvent> events = listAppender.getEvents(); + assertNotNull("No events returned", events); + assertTrue("No events returned", events.size() > 0); + assertTrue("Incorrect event", "Test Message".equals(events.get(0).getMessage().getFormattedMessage())); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueTest.java ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueTest.java b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueTest.java new file mode 100644 index 0000000..a36e148 --- /dev/null +++ b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueTest.java @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.mom.jms.appender; + +import java.util.List; +import java.util.Map; + +import javax.naming.Context; +import javax.naming.InitialContext; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.ConsoleAppender; +import org.apache.logging.log4j.core.filter.AbstractFilter; +import org.apache.logging.log4j.core.filter.CompositeFilter; +import org.apache.logging.log4j.core.layout.PatternLayout; +import org.apache.logging.log4j.mom.jms.receiver.AbstractJmsReceiver; +import org.apache.logging.log4j.mom.jms.receiver.JmsQueueReceiver; +import org.apache.logging.log4j.mom.jms.receiver.JmsTopicReceiver; +import org.apache.logging.log4j.status.StatusConsoleListener; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockejb.jms.MockQueue; +import org.mockejb.jms.QueueConnectionFactoryImpl; +import org.mockejb.jndi.MockContextFactory; + +import static org.junit.Assert.*; + +/** + * + */ +public class JmsQueueTest { + + private static final String FACTORY_NAME = "TestQueueConnectionFactory"; + private static final String QUEUE_NAME = "TestQueue"; + + private static Context context; + private static AbstractJmsReceiver receiver; + + LoggerContext ctx = (LoggerContext) LogManager.getContext(); + Logger root = ctx.getLogger("JmsQueueTest"); + + @BeforeClass + public static void setupClass() throws Exception { + // MockContextFactory becomes the primary JNDI provider + final StatusConsoleListener listener = new StatusConsoleListener(Level.ERROR); + StatusLogger.getLogger().registerListener(listener); + MockContextFactory.setAsInitial(); + context = new InitialContext(); + context.rebind(FACTORY_NAME, new QueueConnectionFactoryImpl()); + context.rebind(QUEUE_NAME, new MockQueue(QUEUE_NAME)); + ((LoggerContext) LogManager.getContext()).reconfigure(); + receiver = new JmsQueueReceiver(FACTORY_NAME, QUEUE_NAME, null, null); + } + + @AfterClass + public static void cleanupClass() { + StatusLogger.getLogger().reset(); + } + + @After + public void teardown() { + final Map<String,Appender> map = root.getAppenders(); + for (final Map.Entry<String, Appender> entry : map.entrySet()) { + final Appender app = entry.getValue(); + root.removeAppender(app); + app.stop(); + } + } + + @Test + public void testServer() throws Exception { + final Filter clientFilter = new MessageFilter(Filter.Result.NEUTRAL, Filter.Result.DENY); + final Filter serverFilter = new MessageFilter(Filter.Result.DENY, Filter.Result.NEUTRAL); + final CompositeFilter clientFilters = CompositeFilter.createFilters(new Filter[]{clientFilter}); + final JmsQueueAppender appender = JmsQueueAppender.createAppender("Test", null, null, null, null, null, FACTORY_NAME, + QUEUE_NAME, null, null, null, clientFilters, "true"); + appender.start(); + final CompositeFilter serverFilters = CompositeFilter.createFilters(new Filter[]{serverFilter}); + final ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false); + listApp.start(); + final PatternLayout layout = PatternLayout.newBuilder().withPattern("%m %ex%n").build(); + final ConsoleAppender console = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true"); + console.start(); + final Logger serverLogger = ctx.getLogger(JmsTopicReceiver.class.getName()); + serverLogger.addAppender(console); + serverLogger.setAdditive(false); + + + // set appender on root and set level to debug + root.addAppender(listApp); + root.addAppender(appender); + root.setAdditive(false); + root.setLevel(Level.DEBUG); + root.debug("This is a test message"); + Thread.sleep(100); + final List<LogEvent> events = listApp.getEvents(); + assertNotNull("No event retrieved", events); + assertTrue("No events retrieved", events.size() > 0); + assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals("This is a test message")); + } + + private class MessageFilter extends AbstractFilter { + + private static final long serialVersionUID = 1L; + + public MessageFilter(final Result onMatch, final Result onMismatch) { + super(onMatch, onMismatch); + } + + @Override + public Result filter(final LogEvent event) { + final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); + for (final StackTraceElement element : stackTrace) { + if (element.getMethodName().equals("onMessage")) { + return onMatch; + } else if (element.getMethodName().equals("testServer")) { + return onMismatch; + } + } + return onMismatch; + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicFailoverTest.java ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicFailoverTest.java b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicFailoverTest.java new file mode 100644 index 0000000..a8de6de --- /dev/null +++ b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicFailoverTest.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.mom.jms.appender; + +import java.util.List; + +import javax.naming.Context; +import javax.naming.InitialContext; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.ThreadContext; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.mom.jms.receiver.AbstractJmsReceiver; +import org.apache.logging.log4j.mom.jms.receiver.JmsTopicReceiver; +import org.apache.logging.log4j.status.StatusConsoleListener; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockejb.jms.MockTopic; +import org.mockejb.jms.TopicConnectionFactoryImpl; +import org.mockejb.jndi.MockContextFactory; + +import static org.junit.Assert.*; + +/** + * + */ +public class JmsTopicFailoverTest { + + private static final String FACTORY_NAME = "TopicConnectionFactory"; + private static final String TOPIC_NAME = "Log4j2Topic"; + + private static Context context; + + private static final String CONFIG = "log4j-jmstopic-failover.xml"; + + private static Configuration config; + private static ListAppender listAppender; + private static LoggerContext ctx; + + @BeforeClass + public static void setupClass() throws Exception { + setupQueue(); + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); + ctx = (LoggerContext) LogManager.getContext(false); + } + + @AfterClass + public static void cleanupClass() { + System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + ctx.reconfigure(); + StatusLogger.getLogger().reset(); + } + + @Before + public void before() { + config = ctx.getConfiguration(); + listAppender = (ListAppender) config.getAppender("List"); + assertNotNull("No Appender", listAppender); + listAppender.clear(); + ThreadContext.clearMap(); + } + + private static void setupQueue() throws Exception { + // MockContextFactory becomes the primary JNDI provider + final StatusConsoleListener listener = new StatusConsoleListener(Level.ERROR); + StatusLogger.getLogger().registerListener(listener); + MockContextFactory.setAsInitial(); + context = new InitialContext(); + context.rebind(FACTORY_NAME, new TopicConnectionFactoryImpl()); + //context.rebind(QUEUE_NAME, new MockQueue(QUEUE_NAME)); + //System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG); + //receiver = new JmsQueueReceiver(FACTORY_NAME, QUEUE_NAME, null, null); + } + + @Test + public void testFailover() throws Exception { + ThreadContext.put("appender", "Failover"); + final Logger logger = LogManager.getLogger(JmsTopicFailoverTest.class); + logger.debug("Test Message"); + final List<LogEvent> events = listAppender.getEvents(); + assertNotNull("No events returned", events); + assertTrue("No events returned", events.size() > 0); + assertTrue("Incorrect event", "Test Message".equals(events.get(0).getMessage().getFormattedMessage())); + } + + @Test + public void testReconnect() throws Exception { + context.rebind(TOPIC_NAME, new MockTopic(TOPIC_NAME)); + final AbstractJmsReceiver receiver = new JmsTopicReceiver(FACTORY_NAME, TOPIC_NAME, null, null); + ThreadContext.put("appender", "Failover"); + final Logger logger = LogManager.getLogger(JmsTopicFailoverTest.class); + logger.debug("Test Message"); + final List<LogEvent> events = listAppender.getEvents(); + assertNotNull("No events returned", events); + assertTrue("No events returned", events.size() > 0); + assertTrue("Incorrect event", "Test Message".equals(events.get(0).getMessage().getFormattedMessage())); + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicTest.java ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicTest.java b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicTest.java new file mode 100644 index 0000000..890f929 --- /dev/null +++ b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicTest.java @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.mom.jms.appender; + +import java.util.List; +import java.util.Map; + +import javax.naming.Context; +import javax.naming.InitialContext; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Appender; +import org.apache.logging.log4j.core.Filter; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.appender.ConsoleAppender; +import org.apache.logging.log4j.core.filter.AbstractFilter; +import org.apache.logging.log4j.core.filter.CompositeFilter; +import org.apache.logging.log4j.core.layout.PatternLayout; +import org.apache.logging.log4j.mom.jms.receiver.AbstractJmsReceiver; +import org.apache.logging.log4j.mom.jms.receiver.JmsTopicReceiver; +import org.apache.logging.log4j.status.StatusConsoleListener; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockejb.jms.MockTopic; +import org.mockejb.jms.TopicConnectionFactoryImpl; +import org.mockejb.jndi.MockContextFactory; + +import static org.junit.Assert.*; + +/** + * + */ +public class JmsTopicTest { + + private static final String FACTORY_NAME = "TestTopicConnectionFactory"; + private static final String TOPIC_NAME = "TestTopic"; + + private static Context context; + private static AbstractJmsReceiver receiver; + + LoggerContext ctx = (LoggerContext) LogManager.getContext(); + Logger root = ctx.getLogger("JmsTopicTest"); + + @BeforeClass + public static void setupClass() throws Exception { + // MockContextFactory becomes the primary JNDI provider + final StatusConsoleListener listener = new StatusConsoleListener(Level.ERROR); + StatusLogger.getLogger().registerListener(listener); + MockContextFactory.setAsInitial(); + context = new InitialContext(); + context.rebind(FACTORY_NAME, new TopicConnectionFactoryImpl()); + context.rebind(TOPIC_NAME, new MockTopic(TOPIC_NAME)); + ((LoggerContext) LogManager.getContext()).reconfigure(); + receiver = new JmsTopicReceiver(FACTORY_NAME, TOPIC_NAME, null, null); + } + + @AfterClass + public static void cleanupClass() { + StatusLogger.getLogger().reset(); + } + + @After + public void teardown() { + final Map<String,Appender> map = root.getAppenders(); + for (final Map.Entry<String, Appender> entry : map.entrySet()) { + final Appender app = entry.getValue(); + root.removeAppender(app); + app.stop(); + } + } + + @Test + public void testServer() throws Exception { + final Filter clientFilter = new MessageFilter(Filter.Result.NEUTRAL, Filter.Result.DENY); + final Filter serverFilter = new MessageFilter(Filter.Result.DENY, Filter.Result.NEUTRAL); + final CompositeFilter clientFilters = CompositeFilter.createFilters(new Filter[]{clientFilter}); + final JmsTopicAppender appender = JmsTopicAppender.createAppender("Test", null, null, null, null, null, FACTORY_NAME, + TOPIC_NAME, null, null, null, clientFilters, "true"); + appender.start(); + final CompositeFilter serverFilters = CompositeFilter.createFilters(new Filter[]{serverFilter}); + final ListAppender listApp = new ListAppender("Events", serverFilters, null, false, false); + listApp.start(); + final PatternLayout layout = PatternLayout.newBuilder().withPattern("%m %ex%n").build(); + final ConsoleAppender console = + ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true"); + console.start(); + final Logger serverLogger = ctx.getLogger(JmsTopicReceiver.class.getName()); + serverLogger.addAppender(console); + serverLogger.setAdditive(false); + + + // set appender on root and set level to debug + root.addAppender(listApp); + root.addAppender(appender); + root.setAdditive(false); + root.setLevel(Level.DEBUG); + root.debug("This is a test message"); + Thread.sleep(100); + final List<LogEvent> events = listApp.getEvents(); + assertNotNull("No event retrieved", events); + assertTrue("No events retrieved", events.size() > 0); + assertTrue("Incorrect event", events.get(0).getMessage().getFormattedMessage().equals("This is a test message")); + } + + private class MessageFilter extends AbstractFilter { + + private static final long serialVersionUID = 1L; + + public MessageFilter(final Result onMatch, final Result onMismatch) { + super(onMatch, onMismatch); + } + + @Override + public Result filter(final LogEvent event) { + final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); + for (final StackTraceElement element : stackTrace) { + if (element.getMethodName().equals("onMessage")) { + return onMatch; + } else if (element.getMethodName().equals("testServer")) { + return onMismatch; + } + } + return onMismatch; + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/resources/JmsAppenderTest.xml ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/resources/JmsAppenderTest.xml b/log4j-jms/src/test/resources/JmsAppenderTest.xml new file mode 100644 index 0000000..ceccbaa --- /dev/null +++ b/log4j-jms/src/test/resources/JmsAppenderTest.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<Configuration name="JmsAppenderTest" status="OFF"> + <Appenders> + <JMS name="JmsQueueAppender" + factoryBindingName="jms/activemq" + destinationBindingName="jms/destination"> + <PatternLayout pattern="%m"/> + </JMS> + </Appenders> + <Loggers> + <Root level="info"> + <AppenderRef ref="JmsQueueAppender"/> + </Root> + </Loggers> +</Configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/resources/log4j-jmsqueue-failover.xml ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/resources/log4j-jmsqueue-failover.xml b/log4j-jms/src/test/resources/log4j-jmsqueue-failover.xml new file mode 100644 index 0000000..8b86750 --- /dev/null +++ b/log4j-jms/src/test/resources/log4j-jmsqueue-failover.xml @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<Configuration status="OFF" name="FailoverTest"> + <Appenders> + <List name="List"/> + <JMSQueue name="Log4j2Queue" queueBindingName="Log4j2Queue" factoryBindingName="QueueConnectionFactory" + ignoreExceptions="false"/> + <Rewrite name="Rewrite" ignoreExceptions="false"> + <PropertiesRewritePolicy> + <Property name="appender">List</Property> + </PropertiesRewritePolicy> + <AppenderRef ref="Log4j2Queue"/> + </Rewrite> + <Failover name="Failover" primary="Rewrite" ignoreExceptions="false"> + <Failovers> + <AppenderRef ref="List"/> + </Failovers> + </Failover> + </Appenders> + + <Loggers> + <Root level="debug"> + <AppenderRef ref="Failover"> + <ThreadContextMapFilter onMatch="ACCEPT" onMismatch="DENY"> + <KeyValuePair key="appender" value="Failover"/> + </ThreadContextMapFilter> + </AppenderRef> + <AppenderRef ref="List"> + <ThreadContextMapFilter onMatch="ACCEPT" onMismatch="DENY"> + <KeyValuePair key="appender" value="List"/> + </ThreadContextMapFilter> + </AppenderRef> + </Root> + </Loggers> + +</Configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/resources/log4j-jmsqueue.xml ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/resources/log4j-jmsqueue.xml b/log4j-jms/src/test/resources/log4j-jmsqueue.xml new file mode 100644 index 0000000..44ea169 --- /dev/null +++ b/log4j-jms/src/test/resources/log4j-jmsqueue.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<Configuration name="ConfigTest" status="OFF" monitorInterval="5"> + <Appenders> + <JMSQueue name="JMSQueue" factoryBindingName="TestQueueConnectionFactory" queueBindingName="TestQueue"> + <PatternLayout pattern="%m%n"/> + </JMSQueue> + </Appenders> + <Loggers> + <Root level="error"> + <AppenderRef ref="JMSQueue"/> + </Root> + </Loggers> +</Configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-jms/src/test/resources/log4j-jmstopic-failover.xml ---------------------------------------------------------------------- diff --git a/log4j-jms/src/test/resources/log4j-jmstopic-failover.xml b/log4j-jms/src/test/resources/log4j-jmstopic-failover.xml new file mode 100644 index 0000000..8588f69 --- /dev/null +++ b/log4j-jms/src/test/resources/log4j-jmstopic-failover.xml @@ -0,0 +1,52 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<Configuration status="OFF" name="FailoverTest"> + <Appenders> + <List name="List"/> + <JMSTopic name="Log4j2Topic" topicBindingName="Log4j2Topic" factoryBindingName="TopicConnectionFactory" + ignoreExceptions="false"/> + <Rewrite name="Rewrite" ignoreExceptions="false"> + <PropertiesRewritePolicy> + <Property name="appender">List</Property> + </PropertiesRewritePolicy> + <AppenderRef ref="Log4j2Topic"/> + </Rewrite> + <Failover name="Failover" primary="Rewrite" ignoreExceptions="false"> + <Failovers> + <AppenderRef ref="List"/> + </Failovers> + </Failover> + </Appenders> + + <Loggers> + <Root level="debug"> + <AppenderRef ref="Failover"> + <ThreadContextMapFilter onMatch="ACCEPT" onMismatch="DENY"> + <KeyValuePair key="appender" value="Failover"/> + </ThreadContextMapFilter> + </AppenderRef> + <AppenderRef ref="List"> + <ThreadContextMapFilter onMatch="ACCEPT" onMismatch="DENY"> + <KeyValuePair key="appender" value="List"/> + </ThreadContextMapFilter> + </AppenderRef> + </Root> + </Loggers> + +</Configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-mom/pom.xml ---------------------------------------------------------------------- diff --git a/log4j-mom/pom.xml b/log4j-mom/pom.xml deleted file mode 100644 index c1fbbe3..0000000 --- a/log4j-mom/pom.xml +++ /dev/null @@ -1,73 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <parent> - <artifactId>log4j</artifactId> - <groupId>org.apache.logging.log4j</groupId> - <version>2.0-SNAPSHOT</version> - </parent> - <modelVersion>4.0.0</modelVersion> - - <artifactId>log4j-mom</artifactId> - <name>Log4j 2 MOM Plugins</name> - <description>Message Oriented Middleware plugins for Log4j 2</description> - - <dependencies> - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-api</artifactId> - </dependency> - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-core</artifactId> - </dependency> - <dependency> - <groupId>org.jboss.spec.javax.jms</groupId> - <artifactId>jboss-jms-api_1.1_spec</artifactId> - </dependency> - <dependency> - <groupId>org.apache.qpid</groupId> - <artifactId>proton-j</artifactId> - </dependency> - - <!-- tests --> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.mockejb</groupId> - <artifactId>mockejb</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - <scope>test</scope> - </dependency> - <!-- integration tests --> - <dependency> - <groupId>org.apache.activemq</groupId> - <artifactId>activemq-broker</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.logging.log4j</groupId> - <artifactId>log4j-core</artifactId> - <type>test-jar</type> - <scope>test</scope> - </dependency> - </dependencies> - - <build> - <plugins> - <plugin> - <groupId>org.apache.felix</groupId> - <artifactId>maven-bundle-plugin</artifactId> - </plugin> - </plugins> - </build> - -</project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsAppender.java ---------------------------------------------------------------------- diff --git a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsAppender.java b/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsAppender.java deleted file mode 100644 index c98e2a1..0000000 --- a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsAppender.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.logging.log4j.mom.jms.appender; - -import java.io.Serializable; -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageProducer; - -import org.apache.logging.log4j.core.Filter; -import org.apache.logging.log4j.core.Layout; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.appender.AbstractAppender; -import org.apache.logging.log4j.core.appender.AppenderLoggingException; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; -import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; -import org.apache.logging.log4j.core.config.plugins.PluginElement; -import org.apache.logging.log4j.core.layout.SerializedLayout; -import org.apache.logging.log4j.mom.jms.manager.JmsManager; -import org.apache.logging.log4j.mom.jms.manager.JndiManager; - -/** - * Generic JMS appender plugin for both queues and topics. - */ -@Plugin(name = "JMS", category = "Core", elementType = "appender", printObject = true) -// TODO: use @PluginAliases to make the separated plugins work through this one -public class JmsAppender extends AbstractAppender { - - private final JmsManager manager; - private final MessageProducer producer; - - protected JmsAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, - final boolean ignoreExceptions, final JmsManager manager) - throws JMSException { - super(name, filter, layout, ignoreExceptions); - this.manager = manager; - this.producer = this.manager.createMessageProducer(); - } - - @Override - public void append(LogEvent event) { - try { - final Message message = this.manager.createMessage(getLayout().toSerializable(event)); - message.setJMSTimestamp(event.getTimeMillis()); - this.producer.send(message); - } catch (JMSException e) { - throw new AppenderLoggingException(e); - } - } - - @PluginBuilderFactory - public static Builder newBuilder() { - return new Builder(); - } - - public static class Builder implements org.apache.logging.log4j.core.util.Builder<JmsAppender> { - - @PluginBuilderAttribute - private String name; - - @PluginBuilderAttribute - private String factoryName; - - @PluginBuilderAttribute - private String providerUrl; - - @PluginBuilderAttribute - private String urlPkgPrefixes; - - @PluginBuilderAttribute - private String securityPrincipalName; - - @PluginBuilderAttribute(sensitive = true) - private String securityCredentials; - - @PluginBuilderAttribute - private String factoryBindingName; - - @PluginBuilderAttribute - private String destinationBindingName; - - @PluginBuilderAttribute - private String username; - - @PluginBuilderAttribute(sensitive = true) - private String password; - - @PluginElement("Layout") - private Layout<? extends Serializable> layout = SerializedLayout.createLayout(); - - @PluginElement("Filter") - private Filter filter; - - @PluginBuilderAttribute - private boolean ignoreExceptions = true; - - private Builder() { - } - - public Builder setName(final String name) { - this.name = name; - return this; - } - - public Builder setFactoryName(final String factoryName) { - this.factoryName = factoryName; - return this; - } - - public Builder setProviderUrl(final String providerUrl) { - this.providerUrl = providerUrl; - return this; - } - - public Builder setUrlPkgPrefixes(final String urlPkgPrefixes) { - this.urlPkgPrefixes = urlPkgPrefixes; - return this; - } - - public Builder setSecurityPrincipalName(final String securityPrincipalName) { - this.securityPrincipalName = securityPrincipalName; - return this; - } - - public Builder setSecurityCredentials(final String securityCredentials) { - this.securityCredentials = securityCredentials; - return this; - } - - public Builder setFactoryBindingName(final String factoryBindingName) { - this.factoryBindingName = factoryBindingName; - return this; - } - - public Builder setDestinationBindingName(final String destinationBindingName) { - this.destinationBindingName = destinationBindingName; - return this; - } - - public Builder setUsername(final String username) { - this.username = username; - return this; - } - - public Builder setPassword(final String password) { - this.password = password; - return this; - } - - public Builder setLayout(final Layout<? extends Serializable> layout) { - this.layout = layout; - return this; - } - - public Builder setFilter(final Filter filter) { - this.filter = filter; - return this; - } - - public Builder setIgnoreExceptions(final boolean ignoreExceptions) { - this.ignoreExceptions = ignoreExceptions; - return this; - } - - @Override - public JmsAppender build() { - final JndiManager jndiManager = JndiManager.getJndiManager(factoryName, providerUrl, urlPkgPrefixes, - securityPrincipalName, securityCredentials, null); - final JmsManager jmsManager = JmsManager.getJmsManager(name, jndiManager, factoryBindingName, - destinationBindingName, username, password); - try { - return new JmsAppender(name, filter, layout, ignoreExceptions, jmsManager); - } catch (final JMSException e) { - LOGGER.error("Error creating JmsAppender [{}].", name, e); - return null; - } - } - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueAppender.java ---------------------------------------------------------------------- diff --git a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueAppender.java b/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueAppender.java deleted file mode 100644 index 4a544e4..0000000 --- a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsQueueAppender.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.logging.log4j.mom.jms.appender; - -import java.io.Serializable; - -import org.apache.logging.log4j.core.Filter; -import org.apache.logging.log4j.core.Layout; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.appender.AbstractAppender; -import org.apache.logging.log4j.core.appender.AppenderLoggingException; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginAttribute; -import org.apache.logging.log4j.core.config.plugins.PluginElement; -import org.apache.logging.log4j.core.config.plugins.PluginFactory; -import org.apache.logging.log4j.core.layout.SerializedLayout; -import org.apache.logging.log4j.mom.jms.manager.JmsQueueManager; -import org.apache.logging.log4j.core.util.Booleans; - -/** - * Appender to write to a JMS Queue. - */ -@Plugin(name = "JMSQueue", category = "Core", elementType = "appender", printObject = true) -public final class JmsQueueAppender extends AbstractAppender { - - private static final long serialVersionUID = 1L; - - private final JmsQueueManager manager; - - private JmsQueueAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, - final JmsQueueManager manager, final boolean ignoreExceptions) { - super(name, filter, layout, ignoreExceptions); - this.manager = manager; - } - - /** - * Actual writing occurs here. - * - * @param event The LogEvent. - */ - @Override - public void append(final LogEvent event) { - try { - manager.send(getLayout().toSerializable(event)); - } catch (final Exception ex) { - throw new AppenderLoggingException(ex); - } - } - - /** - * Create a JmsQueueAppender. - * @param name The name of the Appender. - * @param factoryName The fully qualified class name of the InitialContextFactory. - * @param providerURL The URL of the provider to use. - * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that - * will create a URL context factory - * @param securityPrincipalName The name of the identity of the Principal. - * @param securityCredentials The security credentials of the Principal. - * @param factoryBindingName The name to locate in the Context that provides the QueueConnectionFactory. - * @param queueBindingName The name to use to locate the Queue. - * @param userName The user ID to use to create the Queue Connection. - * @param password The password to use to create the Queue Connection. - * @param layout The layout to use (defaults to SerializedLayout). - * @param filter The Filter or null. - * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise - * they are propagated to the caller. - * @return The JmsQueueAppender. - */ - @PluginFactory - public static JmsQueueAppender createAppender( - @PluginAttribute("name") final String name, - @PluginAttribute("factoryName") final String factoryName, - @PluginAttribute("providerURL") final String providerURL, - @PluginAttribute("urlPkgPrefixes") final String urlPkgPrefixes, - @PluginAttribute("securityPrincipalName") final String securityPrincipalName, - @PluginAttribute("securityCredentials") final String securityCredentials, - @PluginAttribute("factoryBindingName") final String factoryBindingName, - @PluginAttribute("queueBindingName") final String queueBindingName, - @PluginAttribute("userName") final String userName, - @PluginAttribute("password") final String password, - @PluginElement("Layout") Layout<? extends Serializable> layout, - @PluginElement("Filter") final Filter filter, - @PluginAttribute("ignoreExceptions") final String ignore) { - if (name == null) { - LOGGER.error("No name provided for JmsQueueAppender"); - return null; - } - final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); - final JmsQueueManager manager = JmsQueueManager.getJmsQueueManager(factoryName, providerURL, urlPkgPrefixes, - securityPrincipalName, securityCredentials, factoryBindingName, queueBindingName, userName, password); - if (manager == null) { - return null; - } - if (layout == null) { - layout = SerializedLayout.createLayout(); - } - return new JmsQueueAppender(name, filter, layout, manager, ignoreExceptions); - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicAppender.java ---------------------------------------------------------------------- diff --git a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicAppender.java b/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicAppender.java deleted file mode 100644 index 1b000ce..0000000 --- a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsTopicAppender.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.logging.log4j.mom.jms.appender; - -import java.io.Serializable; - -import org.apache.logging.log4j.core.Filter; -import org.apache.logging.log4j.core.Layout; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.appender.AbstractAppender; -import org.apache.logging.log4j.core.appender.AppenderLoggingException; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginAttribute; -import org.apache.logging.log4j.core.config.plugins.PluginElement; -import org.apache.logging.log4j.core.config.plugins.PluginFactory; -import org.apache.logging.log4j.core.layout.SerializedLayout; -import org.apache.logging.log4j.mom.jms.manager.JmsTopicManager; -import org.apache.logging.log4j.core.util.Booleans; - -/** - * Appender to write to a JMS Topic. - */ -@Plugin(name = "JMSTopic", category = "Core", elementType = "appender", printObject = true) -public final class JmsTopicAppender extends AbstractAppender { - - private static final long serialVersionUID = 1L; - - private final JmsTopicManager manager; - - private JmsTopicAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout, - final JmsTopicManager manager, final boolean ignoreExceptions) { - super(name, filter, layout, ignoreExceptions); - this.manager = manager; - } - - /** - * Actual writing occurs here. - * <p/> - * @param event The LogEvent. - */ - @Override - public void append(final LogEvent event) { - try { - manager.send(getLayout().toSerializable(event)); - } catch (final Exception ex) { - throw new AppenderLoggingException(ex); - } - } - - /** - * Create a JmsTopicAppender. - * @param name The name of the Appender. - * @param factoryName The fully qualified class name of the InitialContextFactory. - * @param providerURL The URL of the provider to use. - * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that - * will create a URL context factory - * @param securityPrincipalName The name of the identity of the Principal. - * @param securityCredentials The security credentials of the Principal. - * @param factoryBindingName The name to locate in the Context that provides the TopicConnectionFactory. - * @param topicBindingName The name to use to locate the Topic. - * @param userName The userid to use to create the Topic Connection. - * @param password The password to use to create the Topic Connection. - * @param layout The layout to use (defaults to SerializedLayout). - * @param filter The Filter or null. - * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise - * they are propagated to the caller. - * @return The JmsTopicAppender. - */ - @PluginFactory - public static JmsTopicAppender createAppender( - @PluginAttribute("name") final String name, - @PluginAttribute("factoryName") final String factoryName, - @PluginAttribute("providerURL") final String providerURL, - @PluginAttribute("urlPkgPrefixes") final String urlPkgPrefixes, - @PluginAttribute("securityPrincipalName") final String securityPrincipalName, - @PluginAttribute("securityCredentials") final String securityCredentials, - @PluginAttribute("factoryBindingName") final String factoryBindingName, - @PluginAttribute("topicBindingName") final String topicBindingName, - @PluginAttribute("userName") final String userName, - @PluginAttribute("password") final String password, - @PluginElement("Layout") Layout<? extends Serializable> layout, - @PluginElement("Filter") final Filter filter, - @PluginAttribute("ignoreExceptions") final String ignore) { - - if (name == null) { - LOGGER.error("No name provided for JmsQueueAppender"); - return null; - } - final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); - final JmsTopicManager manager = JmsTopicManager.getJmsTopicManager(factoryName, providerURL, urlPkgPrefixes, - securityPrincipalName, securityCredentials, factoryBindingName, topicBindingName, userName, password); - if (manager == null) { - return null; - } - if (layout == null) { - layout = SerializedLayout.createLayout(); - } - return new JmsTopicAppender(name, filter, layout, manager, ignoreExceptions); - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/package-info.java ---------------------------------------------------------------------- diff --git a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/package-info.java b/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/package-info.java deleted file mode 100644 index 0c7a4ff..0000000 --- a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/appender/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -/** - * Appender classes for using JMS. These are directly configured through your log4j2 configuration file. - */ -package org.apache.logging.log4j.mom.jms.appender; \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/AbstractJmsManager.java ---------------------------------------------------------------------- diff --git a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/AbstractJmsManager.java b/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/AbstractJmsManager.java deleted file mode 100644 index e53b175..0000000 --- a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/AbstractJmsManager.java +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.logging.log4j.mom.jms.manager; - -import java.io.Serializable; -import java.util.Properties; - -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageProducer; -import javax.jms.ObjectMessage; -import javax.jms.Session; -import javax.jms.TextMessage; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NameNotFoundException; -import javax.naming.NamingException; - -import org.apache.logging.log4j.core.appender.AbstractManager; - -/** - * Base Class for Managers of JMS connections. - */ -public abstract class AbstractJmsManager extends AbstractManager { - - /** - * The Constructor. - * @param name The name of the Appender. - */ - public AbstractJmsManager(final String name) { - super(name); - } - - /** - * Create the InitialContext. - * @param factoryName The fully qualified class name of the InitialContextFactory. - * @param providerURL The URL of the provider to use. - * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that - * will create a URL context factory - * @param securityPrincipalName The name of the identity of the Principal. - * @param securityCredentials The security credentials of the Principal. - * @return the InitialContext. - * @throws NamingException if a naming error occurs. - */ - protected static Context createContext(final String factoryName, final String providerURL, - final String urlPkgPrefixes, final String securityPrincipalName, - final String securityCredentials) - throws NamingException { - - final Properties props = getEnvironment(factoryName, providerURL, urlPkgPrefixes, securityPrincipalName, - securityCredentials); - return new InitialContext(props); - } - - /** - * Looks up the name in the context. - * @param ctx The Context. - * @param name The name to locate. - * @return The object to be located. - * @throws NamingException If an error occurs locating the name. - */ - protected static Object lookup(final Context ctx, final String name) throws NamingException { - try { - return ctx.lookup(name); - } catch (final NameNotFoundException e) { - LOGGER.warn("Could not find name [{}].", name); - throw e; - } - } - - /** - * Sets up the properties to pass to the InitialContext. - * @param factoryName The fully qualified class name of the InitialContextFactory. - * @param providerURL The URL of the provider to use. - * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory class that - * will create a URL context factory - * @param securityPrincipalName The name of the identity of the Principal. - * @param securityCredentials The security credentials of the Principal. - * @return The Properties. - * @see javax.naming.Context - */ - protected static Properties getEnvironment(final String factoryName, final String providerURL, - final String urlPkgPrefixes, final String securityPrincipalName, - final String securityCredentials) { - final Properties props = new Properties(); - if (factoryName != null) { - props.setProperty(Context.INITIAL_CONTEXT_FACTORY, factoryName); - if (providerURL != null) { - props.setProperty(Context.PROVIDER_URL, providerURL); - } else { - LOGGER.warn("The InitialContext factory name has been provided without a ProviderURL. " + - "This is likely to cause problems"); - } - if (urlPkgPrefixes != null) { - props.setProperty(Context.URL_PKG_PREFIXES, urlPkgPrefixes); - } - if (securityPrincipalName != null) { - props.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipalName); - if (securityCredentials != null) { - props.setProperty(Context.SECURITY_CREDENTIALS, securityCredentials); - } else { - LOGGER.warn("SecurityPrincipalName has been set without SecurityCredentials. " + - "This is likely to cause problems."); - } - } - return props; - } - return null; - } - - /** - * Send the message. - * @param object The Object to sent. - * @throws Exception if an error occurs. - */ - public abstract void send(Serializable object) throws Exception; - - /** - * Send the Object. - * @param object The Object to send. - * @param session The Session. - * @param producer The MessageProducer. - * @throws Exception if an error occurs. - */ - public synchronized void send(final Serializable object, final Session session, final MessageProducer producer) - throws Exception { - try { - Message msg; - if (object instanceof String) { - msg = session.createTextMessage(); - ((TextMessage) msg).setText((String) object); - } else { - msg = session.createObjectMessage(); - ((ObjectMessage) msg).setObject(object); - } - producer.send(msg); - } catch (final JMSException ex) { - LOGGER.error("Could not publish message via JMS {}", getName()); - throw ex; - } - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/89304e88/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/JmsManager.java ---------------------------------------------------------------------- diff --git a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/JmsManager.java b/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/JmsManager.java deleted file mode 100644 index d3ce550..0000000 --- a/log4j-mom/src/main/java/org/apache/logging/log4j/mom/jms/manager/JmsManager.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache license, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the license for the specific language governing permissions and - * limitations under the license. - */ -package org.apache.logging.log4j.mom.jms.manager; - -import java.io.Serializable; -import javax.jms.Connection; -import javax.jms.ConnectionFactory; -import javax.jms.Destination; -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageConsumer; -import javax.jms.MessageProducer; -import javax.jms.Session; -import javax.naming.NamingException; - -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.core.appender.AbstractManager; -import org.apache.logging.log4j.core.appender.ManagerFactory; -import org.apache.logging.log4j.status.StatusLogger; - -/** - * JMS connection and session manager. Can be used to access MessageProducer, MessageConsumer, and Message objects - * involving a configured ConnectionFactory and Destination. - */ -public class JmsManager extends AbstractManager { - - private static final Logger LOGGER = StatusLogger.getLogger(); - - private static final JmsManagerFactory FACTORY = new JmsManagerFactory(); - - private final JndiManager jndiManager; - private final Connection connection; - private final Session session; - private final Destination destination; - - private JmsManager(final String name, final JndiManager jndiManager, final String connectionFactoryName, - final String destinationName, final String username, final String password) - throws NamingException, JMSException { - super(name); - this.jndiManager = jndiManager; - final ConnectionFactory connectionFactory = this.jndiManager.lookup(connectionFactoryName); - if (username != null && password != null) { - this.connection = connectionFactory.createConnection(username, password); - } else { - this.connection = connectionFactory.createConnection(); - } - this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - this.destination = this.jndiManager.lookup(destinationName); - this.connection.start(); - } - - /** - * Gets a JmsManager using the specified configuration parameters. - * - * @param name The name to use for this JmsManager. - * @param jndiManager The JndiManager to look up JMS information through. - * @param connectionFactoryName The binding name for the {@link javax.jms.ConnectionFactory}. - * @param destinationName The binding name for the {@link javax.jms.Destination}. - * @param username The username to connect with or {@code null} for no authentication. - * @param password The password to use with the given username or {@code null} for no authentication. - * @return The JmsManager as configured. - */ - public static JmsManager getJmsManager(final String name, final JndiManager jndiManager, - final String connectionFactoryName, final String destinationName, - final String username, final String password) { - final JmsConfiguration configuration = new JmsConfiguration(jndiManager, connectionFactoryName, destinationName, - username, password); - return FACTORY.createManager(name, configuration); - } - - /** - * Creates a MessageConsumer on this Destination using the current Session. - * - * @return A MessageConsumer on this Destination. - * @throws JMSException - */ - public MessageConsumer createMessageConsumer() throws JMSException { - return this.session.createConsumer(this.destination); - } - - /** - * Creates a MessageProducer on this Destination using the current Session. - * - * @return A MessageProducer on this Destination. - * @throws JMSException - */ - public MessageProducer createMessageProducer() throws JMSException { - return this.session.createProducer(this.destination); - } - - /** - * Creates a TextMessage or ObjectMessage from a Serializable object. For instance, when using a text-based - * {@link org.apache.logging.log4j.core.Layout} such as {@link org.apache.logging.log4j.core.layout.PatternLayout}, - * the {@link org.apache.logging.log4j.core.LogEvent} message will be serialized to a String. When using a - * layout such as {@link org.apache.logging.log4j.core.layout.SerializedLayout}, the LogEvent message will be - * serialized as a Java object. - * - * @param object The LogEvent or String message to wrap. - * @return A new JMS message containing the provided object. - * @throws JMSException - */ - public Message createMessage(final Serializable object) throws JMSException { - if (object instanceof String) { - return this.session.createTextMessage((String) object); - } else { - return this.session.createObjectMessage(object); - } - } - - @Override - protected void releaseSub() { - try { - this.session.close(); - } catch (final JMSException ignored) { - } - try { - this.connection.close(); - } catch (final JMSException ignored) { - } - this.jndiManager.release(); - } - - private static class JmsConfiguration { - private final JndiManager jndiManager; - private final String connectionFactoryName; - private final String destinationName; - private final String username; - private final String password; - - private JmsConfiguration(JndiManager jndiManager, String connectionFactoryName, String destinationName, - String username, String password) { - this.jndiManager = jndiManager; - this.connectionFactoryName = connectionFactoryName; - this.destinationName = destinationName; - this.username = username; - this.password = password; - } - } - - private static class JmsManagerFactory implements ManagerFactory<JmsManager, JmsConfiguration> { - - @Override - public JmsManager createManager(String name, JmsConfiguration data) { - try { - return new JmsManager(name, data.jndiManager, data.connectionFactoryName, data.destinationName, - data.username, data.password); - } catch (final Exception e) { - LOGGER.error("Error creating JmsManager using ConnectionFactory [{}] and Destination [{}].", - data.connectionFactoryName, data.destinationName, e); - return null; - } - } - } - -}
