-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add a Jakarta JMS Appender #2295 #3247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
garydgregory
merged 1 commit into
apache:2.x
from
garydgregory:2.x_2295_add_JMS_Jakarta_Appender
Dec 7, 2024
+1,127
−12
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This file is here to activate the `plugin-processing` Maven profile. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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. | ||
--> | ||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>${revision}</version> | ||
<relativePath>../log4j-parent</relativePath> | ||
</parent> | ||
<artifactId>log4j-jakarta-jms</artifactId> | ||
<name>Apache Log4j Jakarta JMS</name> | ||
<description>Apache Log4j Java Message Service (JMS), version for Jakarta.</description> | ||
<properties> | ||
<!-- This is a new module in 2.25.0, remove this property after the release. --> | ||
<bnd.baseline.skip>true</bnd.baseline.skip> | ||
<!-- OSGi and JPMS options --> | ||
<bnd-module-name>org.apache.logging.log4j.jakarta.jms</bnd-module-name> | ||
<Fragment-Host>org.apache.logging.log4j.core</Fragment-Host> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>jakarta.jms</groupId> | ||
<artifactId>jakarta.jms-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<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.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-logging</groupId> | ||
<artifactId>commons-logging</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
242 changes: 242 additions & 0 deletions
242
...rta-jms/src/main/java/org/apache/logging/log4j/core/appender/mom/jakarta/JmsAppender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
/* | ||
* 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.jakarta; | ||
|
||
import jakarta.jms.JMSException; | ||
import java.io.Serializable; | ||
import java.util.Properties; | ||
import java.util.concurrent.TimeUnit; | ||
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.AbstractManager; | ||
import org.apache.logging.log4j.core.appender.mom.jakarta.JmsManager.JmsManagerConfiguration; | ||
import org.apache.logging.log4j.core.config.Node; | ||
import org.apache.logging.log4j.core.config.Property; | ||
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.validation.constraints.Required; | ||
import org.apache.logging.log4j.core.net.JndiManager; | ||
|
||
/** | ||
* Jakarta JMS Appender plugin for both queues and topics. | ||
*/ | ||
@Plugin(name = "JMS-Jakarta", category = Node.CATEGORY, elementType = Appender.ELEMENT_TYPE, printObject = true) | ||
public final class JmsAppender extends AbstractAppender { | ||
|
||
public static final class Builder extends AbstractAppender.Builder<Builder> | ||
implements org.apache.logging.log4j.core.util.Builder<JmsAppender> { | ||
|
||
public static final int DEFAULT_RECONNECT_INTERVAL_MILLIS = 5000; | ||
|
||
@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 jakarta.jms.ConnectionFactory JNDI name must be specified") | ||
private String factoryBindingName; | ||
|
||
@PluginBuilderAttribute | ||
@PluginAliases({"queueBindingName", "topicBindingName"}) | ||
@Required(message = "A jakarta.jms.Destination JNDI name must be specified") | ||
private String destinationBindingName; | ||
|
||
@PluginBuilderAttribute | ||
private String userName; | ||
|
||
@PluginBuilderAttribute(sensitive = true) | ||
private char[] password; | ||
|
||
@PluginBuilderAttribute | ||
private long reconnectIntervalMillis = DEFAULT_RECONNECT_INTERVAL_MILLIS; | ||
|
||
@PluginBuilderAttribute | ||
private boolean immediateFail; | ||
|
||
// Programmatic access only for now. | ||
private JmsManager jmsManager; | ||
|
||
private Builder() {} | ||
|
||
@SuppressWarnings("resource") // actualJmsManager and jndiManager are managed by the JmsAppender | ||
@Override | ||
public JmsAppender build() { | ||
JmsManager actualJmsManager = jmsManager; | ||
JmsManagerConfiguration configuration = null; | ||
if (actualJmsManager == null) { | ||
final Properties jndiProperties = JndiManager.createProperties( | ||
factoryName, providerUrl, urlPkgPrefixes, securityPrincipalName, securityCredentials, null); | ||
configuration = new JmsManagerConfiguration( | ||
jndiProperties, | ||
factoryBindingName, | ||
destinationBindingName, | ||
userName, | ||
password, | ||
immediateFail, | ||
reconnectIntervalMillis); | ||
actualJmsManager = AbstractManager.getManager(getName(), JmsManager.FACTORY, configuration); | ||
} | ||
if (actualJmsManager == null) { | ||
// JmsManagerFactory has already logged an ERROR. | ||
return null; | ||
} | ||
final Layout<? extends Serializable> layout = getLayout(); | ||
if (layout == null) { | ||
LOGGER.error("No layout provided for JmsAppender"); | ||
return null; | ||
} | ||
try { | ||
return new JmsAppender( | ||
getName(), getFilter(), layout, isIgnoreExceptions(), getPropertyArray(), actualJmsManager); | ||
} catch (final JMSException e) { | ||
// Never happens since the ctor no longer actually throws a JMSException. | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
public Builder setDestinationBindingName(final String destinationBindingName) { | ||
this.destinationBindingName = destinationBindingName; | ||
return this; | ||
} | ||
|
||
public Builder setFactoryBindingName(final String factoryBindingName) { | ||
this.factoryBindingName = factoryBindingName; | ||
return this; | ||
} | ||
|
||
public Builder setFactoryName(final String factoryName) { | ||
this.factoryName = factoryName; | ||
return this; | ||
} | ||
|
||
public Builder setImmediateFail(final boolean immediateFail) { | ||
this.immediateFail = immediateFail; | ||
return this; | ||
} | ||
|
||
public Builder setJmsManager(final JmsManager jmsManager) { | ||
this.jmsManager = jmsManager; | ||
return this; | ||
} | ||
|
||
public Builder setPassword(final char[] password) { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
public Builder setProviderUrl(final String providerUrl) { | ||
this.providerUrl = providerUrl; | ||
return this; | ||
} | ||
|
||
public Builder setReconnectIntervalMillis(final long reconnectIntervalMillis) { | ||
this.reconnectIntervalMillis = reconnectIntervalMillis; | ||
return this; | ||
} | ||
|
||
public Builder setSecurityCredentials(final String securityCredentials) { | ||
this.securityCredentials = securityCredentials; | ||
return this; | ||
} | ||
|
||
public Builder setSecurityPrincipalName(final String securityPrincipalName) { | ||
this.securityPrincipalName = securityPrincipalName; | ||
return this; | ||
} | ||
|
||
public Builder setUrlPkgPrefixes(final String urlPkgPrefixes) { | ||
this.urlPkgPrefixes = urlPkgPrefixes; | ||
return this; | ||
} | ||
|
||
public Builder setUserName(final String userName) { | ||
this.userName = userName; | ||
return this; | ||
} | ||
|
||
/** | ||
* Does not include the password. | ||
*/ | ||
@Override | ||
public String toString() { | ||
return "Builder [name=" + getName() + ", factoryName=" + factoryName + ", providerUrl=" + providerUrl | ||
+ ", urlPkgPrefixes=" + urlPkgPrefixes + ", securityPrincipalName=" + securityPrincipalName | ||
+ ", securityCredentials=" + securityCredentials + ", factoryBindingName=" + factoryBindingName | ||
+ ", destinationBindingName=" + destinationBindingName + ", username=" + userName + ", layout=" | ||
+ getLayout() + ", filter=" + getFilter() + ", ignoreExceptions=" + isIgnoreExceptions() | ||
+ ", jmsManager=" + jmsManager + "]"; | ||
} | ||
} | ||
|
||
@PluginBuilderFactory | ||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
private volatile JmsManager manager; | ||
|
||
/** | ||
* Constructs a new instance. | ||
* | ||
* @throws JMSException not thrown as of 2.9 but retained in the signature for compatibility, will be removed in 3.0 | ||
*/ | ||
private JmsAppender( | ||
final String name, | ||
final Filter filter, | ||
final Layout<? extends Serializable> layout, | ||
final boolean ignoreExceptions, | ||
final Property[] properties, | ||
final JmsManager manager) | ||
throws JMSException { | ||
super(name, filter, layout, ignoreExceptions, properties); | ||
this.manager = manager; | ||
} | ||
|
||
@Override | ||
public void append(final LogEvent event) { | ||
this.manager.send(event, toSerializable(event)); | ||
} | ||
|
||
public JmsManager getManager() { | ||
return manager; | ||
} | ||
|
||
@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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.