1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.core.appender.mom;
19
20 import java.io.Serializable;
21 import java.util.concurrent.TimeUnit;
22
23 import javax.jms.Connection;
24 import javax.jms.ConnectionFactory;
25 import javax.jms.Destination;
26 import javax.jms.JMSException;
27 import javax.jms.Message;
28 import javax.jms.MessageConsumer;
29 import javax.jms.MessageProducer;
30 import javax.jms.Session;
31 import javax.naming.NamingException;
32
33 import org.apache.logging.log4j.Logger;
34 import org.apache.logging.log4j.core.appender.AbstractManager;
35 import org.apache.logging.log4j.core.appender.ManagerFactory;
36 import org.apache.logging.log4j.core.net.JndiManager;
37 import org.apache.logging.log4j.status.StatusLogger;
38
39
40
41
42
43 public class JmsManager extends AbstractManager {
44
45 private static final Logger LOGGER = StatusLogger.getLogger();
46
47 private static final JmsManagerFactory FACTORY = new JmsManagerFactory();
48
49 private final JndiManager jndiManager;
50 private final Connection connection;
51 private final Session session;
52 private final Destination destination;
53
54 private JmsManager(final String name, final JndiManager jndiManager, final String connectionFactoryName,
55 final String destinationName, final String username, final String password)
56 throws NamingException, JMSException {
57 super(null, name);
58 this.jndiManager = jndiManager;
59 final ConnectionFactory connectionFactory = this.jndiManager.lookup(connectionFactoryName);
60 if (username != null && password != null) {
61 this.connection = connectionFactory.createConnection(username, password);
62 } else {
63 this.connection = connectionFactory.createConnection();
64 }
65 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
66 this.destination = this.jndiManager.lookup(destinationName);
67 this.connection.start();
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81 public static JmsManager getJmsManager(final String name, final JndiManager jndiManager,
82 final String connectionFactoryName, final String destinationName,
83 final String username, final String password) {
84 final JmsConfiguration configuration = new JmsConfiguration(jndiManager, connectionFactoryName, destinationName,
85 username, password);
86 return getManager(name, FACTORY, configuration);
87 }
88
89
90
91
92
93
94
95 public MessageConsumer createMessageConsumer() throws JMSException {
96 return this.session.createConsumer(this.destination);
97 }
98
99
100
101
102
103
104
105 public MessageProducer createMessageProducer() throws JMSException {
106 return this.session.createProducer(this.destination);
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120 public Message createMessage(final Serializable object) throws JMSException {
121 if (object instanceof String) {
122 return this.session.createTextMessage((String) object);
123 }
124 return this.session.createObjectMessage(object);
125 }
126
127 @Override
128 protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
129 boolean closed = true;
130 try {
131 this.session.close();
132 } catch (final JMSException ignored) {
133
134 closed = false;
135 }
136 try {
137 this.connection.close();
138 } catch (final JMSException ignored) {
139
140 closed = false;
141 }
142 return closed && this.jndiManager.stop(timeout, timeUnit);
143 }
144
145 private static class JmsConfiguration {
146 private final JndiManager jndiManager;
147 private final String connectionFactoryName;
148 private final String destinationName;
149 private final String username;
150 private final String password;
151
152 private JmsConfiguration(final JndiManager jndiManager, final String connectionFactoryName, final String destinationName,
153 final String username, final String password) {
154 this.jndiManager = jndiManager;
155 this.connectionFactoryName = connectionFactoryName;
156 this.destinationName = destinationName;
157 this.username = username;
158 this.password = password;
159 }
160 }
161
162 private static class JmsManagerFactory implements ManagerFactory<JmsManager, JmsConfiguration> {
163
164 @Override
165 public JmsManager createManager(final String name, final JmsConfiguration data) {
166 try {
167 return new JmsManager(name, data.jndiManager, data.connectionFactoryName, data.destinationName,
168 data.username, data.password);
169 } catch (final Exception e) {
170 LOGGER.error("Error creating JmsManager using ConnectionFactory [{}] and Destination [{}].",
171 data.connectionFactoryName, data.destinationName, e);
172 return null;
173 }
174 }
175 }
176
177 }