Skip to content

Commit 4804dce

Browse files
committed
Add support for username and password in ActiveMQ connection
1 parent 36981ad commit 4804dce

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ After that step is completed, restart your server and you should have the new in
4141
|--------------------|--------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|
4242
| brokerUrl | failover:(ssl://activemq-1.example.com:61616,ssl://activemq-2.example.com:61616)?randomize=false&backup=true | The ActiveMQ client URL. Any valid ActiveMQ client URL can be used. |
4343
| queueName | ch.qos.logback | The Queue name to read logs off of. |
44+
| username | loguser | The username to use to establish the connection to ActiveMQ. |
45+
| password | myPassw0rd | The password to use to establish the connection to ActiveMQ. |
4446
| throttling_allowed | true | If the graylog server gets busy, slow down this input. Recommend checking this box. |

src/main/java/com/github/exabrial/graylog/OpenwireConsumer.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
public class OpenwireConsumer {
2626
private final String brokerUrl;
27+
private final String username;
28+
private final String password;
2729
private final String queue;
2830
private final MessageInput sourceInput;
2931
private final OpenwireTransport openwireTransport;
@@ -33,9 +35,11 @@ public class OpenwireConsumer {
3335
private AtomicLong lastSecBytesReadTmp = new AtomicLong(0);
3436
private Connection connection;
3537

36-
OpenwireConsumer(String brokerUrl, String queue, MessageInput sourceInput, ScheduledExecutorService scheduler,
37-
OpenwireTransport openwireTransport) {
38+
OpenwireConsumer(String brokerUrl, String username, String password, String queue, MessageInput sourceInput,
39+
ScheduledExecutorService scheduler, OpenwireTransport openwireTransport) {
3840
this.brokerUrl = brokerUrl;
41+
this.username = username;
42+
this.password = password;
3943
this.queue = queue;
4044
this.sourceInput = sourceInput;
4145
this.openwireTransport = openwireTransport;
@@ -88,7 +92,12 @@ public void onMessage(Message message) {
8892
}
8993

9094
public void connect() throws JMSException {
91-
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
95+
ActiveMQConnectionFactory connectionFactory;
96+
if (username != null && !"".equals(username)) {
97+
connectionFactory = new ActiveMQConnectionFactory(username, password, brokerUrl);
98+
} else {
99+
connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
100+
}
92101
connection = connectionFactory.createConnection();
93102
connection.start();
94103
connection.setExceptionListener(new ExceptionListener() {

src/main/java/com/github/exabrial/graylog/OpenwireTransport.java

+22-6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,16 @@
3030
import com.google.inject.assistedinject.AssistedInject;
3131

3232
public class OpenwireTransport extends ThrottleableTransport {
33-
public static final String brokerUrl = "ActiveMQ Broker URL";
34-
public static final String queueName = "Queue Name";
33+
private static final String BROKER_URL_CONFIGURATION_KEY = "brokerUrl";
34+
private static final String USERNAME_CONFIGURATION_KEY = "username";
35+
private static final String PASSWORD_CONFIGURATION_KEY = "password";
36+
private static final String QUEUE_NAME_CONFIGURATION_KEY = "queueName";
37+
38+
private static final String BROKER_URL_CONFIGURATION_HUMAN_NAME = "ActiveMQ Broker URL";
39+
private static final String QUEUE_NAME_CONFIGURATION_HUMAN_NAME = "Queue Name";
40+
private static final String USERNAME_CONFIGURATION_HUMAN_NAME = "Username";
41+
private static final String PASSWORD_CONFIGURATION_HUMAN_NAME = "Password";
42+
3543
private static final Logger log = LoggerFactory.getLogger(OpenwireTransport.class);
3644

3745
private final Configuration configuration;
@@ -115,7 +123,10 @@ public void setMessageAggregator(final CodecAggregator aggregator) {
115123

116124
@Override
117125
public void doLaunch(final MessageInput input) throws MisfireException {
118-
consumer = new OpenwireConsumer(configuration.getString("brokerUrl"), configuration.getString("queueName"), input, scheduler,
126+
consumer = new OpenwireConsumer(configuration.getString(BROKER_URL_CONFIGURATION_KEY),
127+
configuration.getString(USERNAME_CONFIGURATION_KEY),
128+
configuration.getString(PASSWORD_CONFIGURATION_KEY),
129+
configuration.getString(QUEUE_NAME_CONFIGURATION_KEY), input, scheduler,
119130
this);
120131
eventBus.register(this);
121132
try {
@@ -157,11 +168,16 @@ public static class Config extends ThrottleableTransport.Config {
157168
@Override
158169
public ConfigurationRequest getRequestedConfiguration() {
159170
final ConfigurationRequest cr = super.getRequestedConfiguration();
160-
cr.addField(new TextField("brokerUrl", brokerUrl, defaultBrokerUrl(),
171+
cr.addField(new TextField(BROKER_URL_CONFIGURATION_KEY, BROKER_URL_CONFIGURATION_HUMAN_NAME, defaultBrokerUrl(),
161172
"ActiveMQ Broker URL to connect to; Reference the ActiveMQ documentation for help",
162173
ConfigurationField.Optional.NOT_OPTIONAL));
163-
cr.addField(new TextField("queueName", queueName, defaultQueueName(), "Name of queue to listen on",
174+
cr.addField(new TextField(QUEUE_NAME_CONFIGURATION_KEY, QUEUE_NAME_CONFIGURATION_HUMAN_NAME, defaultQueueName(), "Name of queue to listen on",
164175
ConfigurationField.Optional.NOT_OPTIONAL));
176+
cr.addField(new TextField(USERNAME_CONFIGURATION_KEY, USERNAME_CONFIGURATION_HUMAN_NAME, null,
177+
"username for ActiveMQ broker; Reference the ActiveMQ documentation for help",
178+
ConfigurationField.Optional.OPTIONAL));
179+
cr.addField(new TextField(PASSWORD_CONFIGURATION_KEY, PASSWORD_CONFIGURATION_HUMAN_NAME, null, "password for ActiveMQ broker; Reference the ActiveMQ documentation for help",
180+
ConfigurationField.Optional.OPTIONAL));
165181
return cr;
166182
}
167183

@@ -170,7 +186,7 @@ protected String defaultBrokerUrl() {
170186
}
171187

172188
protected String defaultQueueName() {
173-
return "com.example.logback";
189+
return "ch.qos.logback";
174190
}
175191
}
176192
}

0 commit comments

Comments
 (0)