Skip to content

Commit

Permalink
:fix: improve event bus observability
Browse files Browse the repository at this point in the history
Signed-off-by: riccardomodanese <riccardo.modanese@eurotech.com>
  • Loading branch information
riccardomodanese committed Mar 28, 2024
1 parent ab4b252 commit 7cf07e3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
Expand Up @@ -28,4 +28,6 @@ public interface ServiceEventBusDriver {
public void stop() throws ServiceEventBusException;

public ServiceEventBus getEventBus();

public Boolean isConnected();
}
Expand Up @@ -136,6 +136,10 @@ private void setSession(ServiceEvent kapuaEvent) {
KapuaSession.createFrom(kapuaEvent.getScopeId(), kapuaEvent.getUserId());
}

public Boolean isConnected() {
return eventBusJMSConnectionBridge.isConnected();

Check warning on line 140 in commons/src/main/java/org/eclipse/kapua/commons/event/jms/JMSServiceEventBus.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/event/jms/JMSServiceEventBus.java#L140

Added line #L140 was not covered by tests
}

/**
* Stop the event bus
*
Expand Down Expand Up @@ -209,9 +213,15 @@ private class EventBusJMSConnectionBridge {
private Connection jmsConnection;
private Map<String, SenderPool> senders = new HashMap<>();
private ExceptionListenerImpl exceptionListener;
private Boolean connected;

public EventBusJMSConnectionBridge() {
this.exceptionListener = new ExceptionListenerImpl();
connected = Boolean.FALSE;
}

Boolean isConnected() {
return connected;

Check warning on line 224 in commons/src/main/java/org/eclipse/kapua/commons/event/jms/JMSServiceEventBus.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/event/jms/JMSServiceEventBus.java#L224

Added line #L224 was not covered by tests
}

void start() throws JMSException, NamingException, ServiceEventBusException {
Expand All @@ -230,6 +240,7 @@ void start() throws JMSException, NamingException, ServiceEventBusException {

jmsConnection = jmsConnectionFactory.createConnection(eventbusUsername, eventbusPassword);
jmsConnection.setExceptionListener(exceptionListener);
connected = Boolean.TRUE;

Check warning on line 243 in commons/src/main/java/org/eclipse/kapua/commons/event/jms/JMSServiceEventBus.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/event/jms/JMSServiceEventBus.java#L243

Added line #L243 was not covered by tests
jmsConnection.start();
}

Expand All @@ -239,6 +250,7 @@ void stop() throws ServiceEventBusException {
}

private void closeConnection() {
connected = Boolean.FALSE;
try {
if (jmsConnection != null) {
exceptionListener.stop();
Expand Down Expand Up @@ -436,6 +448,7 @@ private class ExceptionListenerImpl implements ExceptionListener {

@Override
public void onException(JMSException e) {
connected = Boolean.FALSE;

Check warning on line 451 in commons/src/main/java/org/eclipse/kapua/commons/event/jms/JMSServiceEventBus.java

View check run for this annotation

Codecov / codecov/patch

commons/src/main/java/org/eclipse/kapua/commons/event/jms/JMSServiceEventBus.java#L451

Added line #L451 was not covered by tests
LOGGER.error("EventBus Listener {} - Connection thrown exception: {}", this, e.getMessage(), e);
commonsMetric.getEventBusConnectionError().inc();
int i = 1;
Expand Down
Expand Up @@ -32,19 +32,22 @@ protected void configureModule() {
@Singleton
ServiceEventBusDriver serviceEventBusDriver() {
return new ServiceEventBusDriver() {

private Boolean connected = Boolean.FALSE;

@Override
public String getType() {
return "test";
}

@Override
public void start() throws ServiceEventBusException {
//Nothing to do!
connected = Boolean.TRUE;
}

@Override
public void stop() throws ServiceEventBusException {
//Nothing to do!
connected = Boolean.FALSE;
}

@Override
Expand All @@ -61,6 +64,11 @@ public void subscribe(String address, String name, ServiceEventBusListener event
}
};
}

@Override
public Boolean isConnected() {
return connected;
}
};
}
}
Expand Up @@ -47,19 +47,22 @@ String eventsModuleName() {
@Singleton
ServiceEventBusDriver serviceEventBusDriver() {
return new ServiceEventBusDriver() {

private Boolean connected = Boolean.FALSE;

@Override
public String getType() {
return "test";
}

@Override
public void start() throws ServiceEventBusException {
//Nothing to do!
connected = Boolean.TRUE;
}

@Override
public void stop() throws ServiceEventBusException {
//Nothing to do!
connected = Boolean.FALSE;
}

@Override
Expand All @@ -76,6 +79,11 @@ public void subscribe(String address, String name, ServiceEventBusListener event
}
};
}

@Override
public Boolean isConnected() {
return connected;
}
};
}
}

0 comments on commit 7cf07e3

Please sign in to comment.