Repository: logging-log4j2 Updated Branches: refs/heads/master 44bcad3f9 -> d616f08a7
Move ActiveMQ test to its own package like the other MOM provider tests. Update test to use the JMS Appender builder which requires a new programmatic-only (for now) Builder property. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/d616f08a Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/d616f08a Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/d616f08a Branch: refs/heads/master Commit: d616f08a76b0aec128c0e2d4224f948b957465d2 Parents: 44bcad3 Author: Gary Gregory <ggreg...@apache.org> Authored: Wed Nov 2 12:22:04 2016 -0700 Committer: Gary Gregory <ggreg...@apache.org> Committed: Wed Nov 2 12:22:04 2016 -0700 ---------------------------------------------------------------------- .../log4j/core/appender/mom/JmsAppender.java | 442 ++++++++++--------- .../log4j/core/appender/mom/JmsAppenderIT.java | 133 ------ .../appender/mom/activemq/JmsAppenderIT.java | 142 ++++++ 3 files changed, 369 insertions(+), 348 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d616f08a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsAppender.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsAppender.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsAppender.java index 7794b0d..c1732c7 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsAppender.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/mom/JmsAppender.java @@ -1,215 +1,227 @@ -/* - * 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.core.appender.mom; - -import java.io.Serializable; -import java.util.concurrent.TimeUnit; - -import javax.jms.JMSException; -import javax.jms.Message; -import javax.jms.MessageProducer; - -import org.apache.logging.log4j.core.Appender; -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.Node; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginAliases; -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.config.plugins.validation.constraints.Required; -import org.apache.logging.log4j.core.layout.SerializedLayout; -import org.apache.logging.log4j.core.net.JndiManager; - -/** - * Generic JMS Appender plugin for both queues and topics. This Appender replaces the previous split ones. However, - * configurations set up for the 2.0 version of the JMS appenders will still work. - */ -@Plugin(name = "JMS", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true) -@PluginAliases({"JMSQueue", "JMSTopic"}) -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(final LogEvent event) { - try { - final Message message = this.manager.createMessage(getLayout().toSerializable(event)); - message.setJMSTimestamp(event.getTimeMillis()); - this.producer.send(message); - } catch (final JMSException e) { - throw new AppenderLoggingException(e); - } - } - - @Override - public boolean stop(final long timeout, final TimeUnit timeUnit) { - setStopping(); - boolean stopped = super.stop(timeout, timeUnit, false); - stopped &= this.manager.stop(timeout, timeUnit); - setStopped(); - return stopped; - } - - @PluginBuilderFactory - public static Builder newBuilder() { - return new Builder(); - } - - public static class Builder implements org.apache.logging.log4j.core.util.Builder<JmsAppender> { - - @PluginBuilderAttribute - @Required(message = "A name for the JmsAppender must be specified") - 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 - @Required(message = "A javax.jms.ConnectionFactory JNDI name must be specified") - private String factoryBindingName; - - @PluginBuilderAttribute - @PluginAliases({"queueBindingName", "topicBindingName"}) - @Required(message = "A javax.jms.Destination JNDI name must be specified") - 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; - } - } - } - -} +/* + * 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.core.appender.mom; + +import java.io.Serializable; +import java.util.concurrent.TimeUnit; + +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageProducer; + +import org.apache.logging.log4j.core.Appender; +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.Node; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAliases; +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.config.plugins.validation.constraints.Required; +import org.apache.logging.log4j.core.layout.SerializedLayout; +import org.apache.logging.log4j.core.net.JndiManager; + +/** + * Generic JMS Appender plugin for both queues and topics. This Appender replaces the previous split ones. However, + * configurations set up for the 2.0 version of the JMS appenders will still work. + */ +@Plugin(name = "JMS", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true) +@PluginAliases({"JMSQueue", "JMSTopic"}) +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(final LogEvent event) { + try { + final Message message = this.manager.createMessage(getLayout().toSerializable(event)); + message.setJMSTimestamp(event.getTimeMillis()); + this.producer.send(message); + } catch (final JMSException e) { + throw new AppenderLoggingException(e); + } + } + + @Override + public boolean stop(final long timeout, final TimeUnit timeUnit) { + setStopping(); + boolean stopped = super.stop(timeout, timeUnit, false); + stopped &= this.manager.stop(timeout, timeUnit); + setStopped(); + return stopped; + } + + @PluginBuilderFactory + public static Builder newBuilder() { + return new Builder(); + } + + public static class Builder implements org.apache.logging.log4j.core.util.Builder<JmsAppender> { + + @PluginBuilderAttribute + @Required(message = "A name for the JmsAppender must be specified") + 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 + @Required(message = "A javax.jms.ConnectionFactory JNDI name must be specified") + private String factoryBindingName; + + @PluginBuilderAttribute + @PluginAliases({"queueBindingName", "topicBindingName"}) + @Required(message = "A javax.jms.Destination JNDI name must be specified") + 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; + + // Programmatic access only for now. + private JmsManager jmsManager; + + 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 setJmsManager(final JmsManager jmsManager) { + this.jmsManager = jmsManager; + return this; + } + + public Builder setIgnoreExceptions(final boolean ignoreExceptions) { + this.ignoreExceptions = ignoreExceptions; + return this; + } + + @Override + public JmsAppender build() { + JmsManager actualJmsManager = jmsManager; + if (actualJmsManager == null) { + final JndiManager jndiManager = JndiManager.getJndiManager(factoryName, providerUrl, urlPkgPrefixes, + securityPrincipalName, securityCredentials, null); + actualJmsManager = JmsManager.getJmsManager(name, jndiManager, factoryBindingName, + destinationBindingName, username, password); + } + try { + return new JmsAppender(name, filter, layout, ignoreExceptions, actualJmsManager); + } catch (final JMSException e) { + LOGGER.error("Error creating JmsAppender [{}].", name, e); + return null; + } + } + + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/d616f08a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/JmsAppenderIT.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/JmsAppenderIT.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/JmsAppenderIT.java deleted file mode 100644 index 97dafbe..0000000 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/JmsAppenderIT.java +++ /dev/null @@ -1,133 +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.core.appender.mom; - -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.core.net.JndiManager; -import org.apache.logging.log4j.message.SimpleMessage; -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 final String KEY_SERIALIZABLE_PACKAGES = "org.apache.activemq.SERIALIZABLE_PACKAGES"; - - private static JmsManager jmsManager; - - private JmsAppender appender; - - @BeforeClass - public static void setUpClass() { - System.setProperty(KEY_SERIALIZABLE_PACKAGES, - "org.apache.logging.log4j.core.impl,org.apache.logging.log4j.util,org.apache.logging.log4j"); - final Properties additional = new Properties(); - additional.setProperty("queue.TestQueue", "TestQueue"); - final 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.close(); - System.getProperties().remove(KEY_SERIALIZABLE_PACKAGES); - } - - @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.newBuilder().setLoggerName(loggerName) // - .setLoggerFqcn(loggerName).setLevel(Level.INFO) // - .setMessage(new SimpleMessage(messageText)).setThreadName(Thread.currentThread().getName()) // - .setTimeMillis(System.currentTimeMillis()).build(); - 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<>(messageCount); - } - - @Override - public void onMessage(final Message message) { - try { - consume((ObjectMessage) message); - } catch (final JMSException e) { - e.printStackTrace(); - } - } - - private void consume(final 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/d616f08a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/activemq/JmsAppenderIT.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/activemq/JmsAppenderIT.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/activemq/JmsAppenderIT.java new file mode 100644 index 0000000..5e7fd50 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/mom/activemq/JmsAppenderIT.java @@ -0,0 +1,142 @@ +/* + * 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.core.appender.mom.activemq; + +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.appender.mom.JmsAppender; +import org.apache.logging.log4j.core.appender.mom.JmsManager; +import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.core.layout.SerializedLayout; +import org.apache.logging.log4j.core.net.JndiManager; +import org.apache.logging.log4j.message.SimpleMessage; +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 final String KEY_SERIALIZABLE_PACKAGES = "org.apache.activemq.SERIALIZABLE_PACKAGES"; + + private static JmsManager jmsManager; + + private JmsAppender appender; + + @BeforeClass + public static void setUpClass() { + System.setProperty(KEY_SERIALIZABLE_PACKAGES, + "org.apache.logging.log4j.core.impl,org.apache.logging.log4j.util,org.apache.logging.log4j"); + final Properties additional = new Properties(); + additional.setProperty("queue.TestQueue", "TestQueue"); + final 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.close(); + System.getProperties().remove(KEY_SERIALIZABLE_PACKAGES); + } + + @Before + public void setUp() throws Exception { + // @formatter:off + appender = JmsAppender.newBuilder(). + setName("JmsAppender"). + setLayout(SerializedLayout.createLayout()). + setIgnoreExceptions(true). + setJmsManager(jmsManager). + build(); + // @formatter:off + 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.newBuilder().setLoggerName(loggerName) // + .setLoggerFqcn(loggerName).setLevel(Level.INFO) // + .setMessage(new SimpleMessage(messageText)).setThreadName(Thread.currentThread().getName()) // + .setTimeMillis(System.currentTimeMillis()).build(); + 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<>(messageCount); + } + + @Override + public void onMessage(final Message message) { + try { + consume((ObjectMessage) message); + } catch (final JMSException e) { + e.printStackTrace(); + } + } + + private void consume(final 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()); + } + } +}