Skip to content

Commit 8fd917e

Browse files
author
Erlend Viddal
committed
Rettelser etter QA
1 parent a59bca9 commit 8fd917e

File tree

17 files changed

+106
-84
lines changed

17 files changed

+106
-84
lines changed

servicebuilder-core/src/main/java/no/obos/util/servicebuilder/ServiceConfig.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import no.obos.util.servicebuilder.model.PropertyProvider;
1212
import no.obos.util.servicebuilder.model.ServiceDefinition;
1313
import no.obos.util.servicebuilder.util.GuavaHelper;
14-
import org.glassfish.hk2.api.TypeLiteral;
1514
import org.glassfish.hk2.utilities.binding.AbstractBinder;
1615
import org.glassfish.jersey.server.ResourceConfig;
1716

@@ -50,10 +49,6 @@ public <T> ServiceConfig bind(Class<T> toBind) {
5049
return bind(binder -> binder.bindAsContract(toBind));
5150
}
5251

53-
public <T> ServiceConfig bindParameterized(T toBind) {
54-
return bind(binder -> binder.bind(toBind).to(new TypeLiteral<T>() {}));
55-
}
56-
5752
public ServiceConfig bind(Binder binder) {
5853
return withBinders(GuavaHelper.plus(binders, binder));
5954
}

servicebuilder-interfaces/src/main/java/no/obos/util/servicebuilder/annotations/Mq.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

servicebuilder-interfaces/src/main/java/no/obos/util/servicebuilder/model/MessageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* Interface for handling messages to be implemented by application
55
*/
66
public interface MessageHandler<T> {
7-
void handle(T message, MessageMeta meta);
7+
void handle(T message, MessageMetadata meta);
88
}

servicebuilder-interfaces/src/main/java/no/obos/util/servicebuilder/model/MessageMeta.java renamed to servicebuilder-interfaces/src/main/java/no/obos/util/servicebuilder/model/MessageMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
@Builder
1010
@ToString
11-
public class MessageMeta {
11+
public class MessageMetadata {
1212
public final String requestId;
1313
public final String sourceApp;
1414
}

servicebuilder-mq-activemq/src/main/java/no/obos/util/servicebuilder/addon/ActiveMqAddon.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,7 @@ public ActiveMqAddon initialize(ServiceConfig serviceConfig) {
6868
.password(password)
6969
.build();
7070

71-
ActiveMqListener listener = ActiveMqListener.builder()
72-
.mqHandlerForwarder(mqAddon.mqHandlerForwarder)
73-
.activeMqConnectionProvider(connectionProvider)
74-
.handlerDescriptions(mqAddon.handlers)
75-
.build();
71+
ActiveMqListener listener = new ActiveMqListener(connectionProvider, mqAddon.mqHandlerForwarder);
7672

7773
return this
7874
.withMqAddon(mqAddon)
@@ -123,8 +119,8 @@ public void addToJettyServer(JettyServer jettyServer) {
123119
mqAddon.handlers.forEach(handlerDescription -> {
124120
String queueInput = handlerDescription.messageDescription.getQueueName();
125121
String queueError = handlerDescription.messageDescription.getErrorQueueName();
126-
ObosHealthCheckRegistry.registerActiveMqCheck("Input queue: " + queueInput + " on " + url, url, queueInput, handlerDescription.healthCheckEntriesMax, handlerDescription.healthCheckEntriesMax, user, password);
127-
ObosHealthCheckRegistry.registerActiveMqCheck("Error queue: " + queueError + " on " + url, url, queueError, 1, 60, user, password);
122+
ObosHealthCheckRegistry.registerActiveMqCheck("Input queue: " + queueInput + " on " + url, url, queueInput, handlerDescription.healthCheckEntriesMax, handlerDescription.healthCheckEntriesGrace, user, password);
123+
ObosHealthCheckRegistry.registerActiveMqCheck("Error queue: " + queueError + " on " + url, url, queueError, 1, 1, user, password);
128124
ObosHealthCheckRegistry.registerCustomCheck("ActiveMqListener active", () ->
129125
listener.isListenerActive()
130126
? ObosHealthCheckResult.ok()

servicebuilder-mq-activemq/src/main/java/no/obos/util/servicebuilder/mq/activemq/ActiveMqConnectionProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.function.Function;
1515

1616
import static no.obos.util.servicebuilder.mq.activemq.ActiveMqUtils.closeConnection;
17+
import static no.obos.util.servicebuilder.mq.activemq.ActiveMqUtils.closeConnectionNoException;
1718

1819
/**
1920
* Handles connections to activeMQ so other classes wont have to deal with connection-specific information.
@@ -25,12 +26,12 @@ public class ActiveMqConnectionProvider {
2526
private final String user;
2627
private final String password;
2728

28-
public void startListenerSession(BiConsumer<Connection, Session> fun, ErrorCallback onError) {
29+
public void startListenerSession(BiConsumer<Connection, Session> listener, ErrorCallback onError) {
2930
ActiveMQConnection connection = ActiveMqUtils.openConnection(user, password, url);
3031

3132
ExceptionListener onErrorWithClose = (JMSException ex) -> {
3233
log.error("Problem with ActiveMQ session, closing connection and forwarding", ex);
33-
closeConnection(connection);
34+
closeConnectionNoException(connection);
3435
onError.onError();
3536
};
3637
try {
@@ -40,7 +41,7 @@ public void startListenerSession(BiConsumer<Connection, Session> fun, ErrorCallb
4041
throw new MessageQueueException(e);
4142
}
4243
Session session = ActiveMqUtils.startSession(connection);
43-
fun.accept(connection, session);
44+
listener.accept(connection, session);
4445
}
4546

4647
public <T> T inSessionWithReturn(Function<Session, T> fun) {

servicebuilder-mq-activemq/src/main/java/no/obos/util/servicebuilder/mq/activemq/ActiveMqListener.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package no.obos.util.servicebuilder.mq.activemq;
22

33
import com.google.common.collect.ImmutableSet;
4-
import lombok.Builder;
54
import lombok.Getter;
5+
import lombok.RequiredArgsConstructor;
66
import lombok.extern.slf4j.Slf4j;
7-
import no.obos.util.servicebuilder.mq.HandlerDescription;
87
import no.obos.util.servicebuilder.mq.MqHandlerForwarder;
98
import no.obos.util.servicebuilder.mq.MqHandlerImpl;
109
import no.obos.util.servicebuilder.mq.MqListener;
@@ -17,33 +16,26 @@
1716
* Handles connecting several listeners to activemq in single session, reconnection and status for monitoring.
1817
*/
1918
@Slf4j
20-
@Builder
19+
@RequiredArgsConstructor
2120
public class ActiveMqListener implements MqListener {
2221
private final ActiveMqConnectionProvider activeMqConnectionProvider;
2322

24-
private final ImmutableSet<HandlerDescription<?>> handlerDescriptions;
2523
private final MqHandlerForwarder mqHandlerForwarder;
2624

2725
@Getter
2826
private boolean listenerActive = false;
2927

30-
private boolean abort = false;
28+
private boolean threadIsInterrupted = false;
3129

3230
private Connection connection = null;
3331
private Session session = null;
3432

35-
ImmutableSet<MqHandlerImpl<?>> handlers;
36-
37-
public void setHandlers(Iterable<MqHandlerImpl<?>> handlers) {
38-
this.handlers = ImmutableSet.copyOf(handlers);
39-
}
40-
41-
public void startListener() {
33+
public void startListener(ImmutableSet<MqHandlerImpl<?>> handlers) {
4234
log.debug("Starting listener...");
4335
if (listenerActive) {
4436
throw new RuntimeException("Multiple active sessions in same listener. Check if starting connection threw exception and ActiveMQ ActiveMQConnection.setExceptionListener() failed at the same time.");
4537
}
46-
abort = false;
38+
threadIsInterrupted = false;
4739
try {
4840
activeMqConnectionProvider.startListenerSession((connection, session) -> {
4941
this.connection = connection;
@@ -77,10 +69,10 @@ private void restartListener(ImmutableSet<MqHandlerImpl<?>> handlers) {
7769
} catch (InterruptedException e) {
7870
log.info("Interrupted.");
7971
Thread.currentThread().interrupt();
80-
abort = true;
72+
threadIsInterrupted = true;
8173
}
82-
if (! abort) {
83-
startListener();
74+
if (! threadIsInterrupted) {
75+
startListener(handlers);
8476
}
8577
}
8678

servicebuilder-mq-activemq/src/main/java/no/obos/util/servicebuilder/mq/activemq/ActiveMqQueueListener.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ private void handleMessage(Message message, Session session) {
4545
return;
4646
}
4747
TextMessage textMessage = (TextMessage) message;
48-
String text = null;
48+
String text;
4949
try {
5050
text = textMessage.getText();
5151
} catch (JMSException e) {
5252
log.error("Could not read text from message", e);
5353
toErrorQueue(message);
5454
ActiveMqUtils.commitSession(session);
55+
return;
5556
}
5657
try {
5758
mqHandlerForwarder.forward(handler, text);
@@ -73,6 +74,7 @@ private void toErrorQueue(String text) {
7374
errorProducer.close();
7475
} catch (JMSException jmse) {
7576
log.error("Failed to create error message", jmse);
77+
throw new RuntimeException("Error queueing error message, aborting");
7678
}
7779
}
7880

@@ -85,6 +87,7 @@ private void toErrorQueue(Message message) {
8587
errorProducer.close();
8688
} catch (JMSException jmse) {
8789
log.error("Failed to create error message", jmse);
90+
throw new RuntimeException("Error queueing error message, aborting");
8891
}
8992
}
9093

servicebuilder-mq-activemq/src/main/java/no/obos/util/servicebuilder/mq/activemq/ActiveMqUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ static void closeConnection(ActiveMQConnection connection) {
6161
}
6262
}
6363

64+
static void closeConnectionNoException(ActiveMQConnection connection) {
65+
try {
66+
connection.close();
67+
} catch (JMSException ex) {
68+
log.error("Could not close session connection, continuing", ex);
69+
}
70+
}
71+
6472
static void commitSession(Session session) {
6573
try {
6674
session.commit();

servicebuilder-mq-activemq/src/test/java/no/obos/util/servicebuilder/mq/addon/ActiveMqAddonSendReceiveTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import no.obos.util.servicebuilder.addon.ObosLogFilterAddon;
1414
import no.obos.util.servicebuilder.model.Constants;
1515
import no.obos.util.servicebuilder.model.MessageDescription;
16-
import no.obos.util.servicebuilder.model.ServiceDefinition;
1716
import no.obos.util.servicebuilder.model.MessageHandler;
18-
import no.obos.util.servicebuilder.model.MessageMeta;
17+
import no.obos.util.servicebuilder.model.MessageMetadata;
1918
import no.obos.util.servicebuilder.model.MessageSender;
19+
import no.obos.util.servicebuilder.model.ServiceDefinition;
2020
import org.apache.activemq.broker.BrokerService;
2121
import org.junit.Assert;
2222
import org.junit.Test;
@@ -59,13 +59,13 @@ public void sendAndReceiveMessage() {
5959

6060
MyMessageV1 expected = new MyMessageV1(LocalDate.now(), "brillefin");
6161
ArgumentCaptor<MyMessageV1> myMessageV1ArgumentCator = ArgumentCaptor.forClass(MyMessageV1.class);
62-
ArgumentCaptor<MessageMeta> messageMetaArgumentCaptor = ArgumentCaptor.forClass(MessageMeta.class);
62+
ArgumentCaptor<MessageMetadata> messageMetaArgumentCaptor = ArgumentCaptor.forClass(MessageMetadata.class);
6363
TestServiceRunner.defaults(serviceConfig)
6464
.oneShotVoid(MyResource.class, it -> it.addToQueue(expected));
6565
verify(messageHandler).handle(myMessageV1ArgumentCator.capture(), messageMetaArgumentCaptor.capture());
6666

6767
MyMessageV1 actual = myMessageV1ArgumentCator.getValue();
68-
MessageMeta meta = messageMetaArgumentCaptor.getValue();
68+
MessageMetadata meta = messageMetaArgumentCaptor.getValue();
6969

7070
assertThat(actual).isEqualTo(expected);
7171
assertThat(meta.sourceApp).isEqualTo(MyServiceDefinition.instance.getName());

0 commit comments

Comments
 (0)