Use LogAccessor from SF
* Change main classes to use a `LogAccessor` API to simplify code flow * Fix tests according `LogAccessor` property * Fix some Sonar smells
This commit is contained in:
@@ -121,6 +121,7 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
*/
|
||||
public MqttPahoMessageDrivenChannelAdapter(String clientId, MqttPahoClientFactory clientFactory,
|
||||
String... topic) {
|
||||
|
||||
super(null, clientId, topic);
|
||||
this.clientFactory = clientFactory;
|
||||
}
|
||||
@@ -190,7 +191,7 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
String url = getUrl();
|
||||
if (url != null) {
|
||||
options = MqttUtils.cloneConnectOptions(options);
|
||||
options.setServerURIs(new String[] { url });
|
||||
options.setServerURIs(new String[]{ url });
|
||||
}
|
||||
}
|
||||
return options;
|
||||
@@ -203,8 +204,8 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
try {
|
||||
connectAndSubscribe();
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error("Exception while connecting and subscribing, retrying", e);
|
||||
catch (Exception ex) {
|
||||
logger.error(ex, "Exception while connecting and subscribing, retrying");
|
||||
this.scheduleReconnect();
|
||||
}
|
||||
}
|
||||
@@ -222,14 +223,14 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
this.client.unsubscribe(getTopic());
|
||||
}
|
||||
}
|
||||
catch (MqttException e) {
|
||||
logger.error("Exception while unsubscribing", e);
|
||||
catch (MqttException ex) {
|
||||
logger.error(ex, "Exception while unsubscribing");
|
||||
}
|
||||
try {
|
||||
this.client.disconnectForcibly(this.disconnectCompletionTimeout);
|
||||
}
|
||||
catch (MqttException e) {
|
||||
logger.error("Exception while disconnecting", e);
|
||||
catch (MqttException ex) {
|
||||
logger.error(ex, "Exception while disconnecting");
|
||||
}
|
||||
|
||||
this.client.setCallback(null);
|
||||
@@ -237,8 +238,8 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
try {
|
||||
this.client.close();
|
||||
}
|
||||
catch (MqttException e) {
|
||||
logger.error("Exception while closing", e);
|
||||
catch (MqttException ex) {
|
||||
logger.error(ex, "Exception while closing");
|
||||
}
|
||||
this.connected = false;
|
||||
this.client = null;
|
||||
@@ -305,20 +306,18 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
this.client.subscribe(topics, grantedQos);
|
||||
for (int i = 0; i < requestedQos.length; i++) {
|
||||
if (grantedQos[i] != requestedQos[i]) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Granted QOS different to Requested QOS; topics: " + Arrays.toString(topics)
|
||||
+ " requested: " + Arrays.toString(requestedQos)
|
||||
+ " granted: " + Arrays.toString(grantedQos));
|
||||
}
|
||||
logger.warn(() -> "Granted QOS different to Requested QOS; topics: " + Arrays.toString(topics)
|
||||
+ " requested: " + Arrays.toString(requestedQos)
|
||||
+ " granted: " + Arrays.toString(grantedQos));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (MqttException e) {
|
||||
catch (MqttException ex) {
|
||||
if (this.applicationEventPublisher != null) {
|
||||
this.applicationEventPublisher.publishEvent(new MqttConnectionFailedEvent(this, e));
|
||||
this.applicationEventPublisher.publishEvent(new MqttConnectionFailedEvent(this, ex));
|
||||
}
|
||||
logger.error("Error connecting or subscribing to " + Arrays.toString(topics), e);
|
||||
logger.error(ex, () -> "Error connecting or subscribing to " + Arrays.toString(topics));
|
||||
this.client.disconnectForcibly(this.disconnectCompletionTimeout);
|
||||
try {
|
||||
this.client.setCallback(null);
|
||||
@@ -328,7 +327,7 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
// NOSONAR
|
||||
}
|
||||
this.client = null;
|
||||
throw e;
|
||||
throw ex;
|
||||
}
|
||||
finally {
|
||||
this.topicLock.unlock();
|
||||
@@ -336,9 +335,7 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
if (this.client.isConnected()) {
|
||||
this.connected = true;
|
||||
String message = "Connected and subscribed to " + Arrays.toString(topics);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(message);
|
||||
}
|
||||
logger.debug(message);
|
||||
if (this.applicationEventPublisher != null) {
|
||||
this.applicationEventPublisher.publishEvent(new MqttSubscribedEvent(this, message));
|
||||
}
|
||||
@@ -357,9 +354,7 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
try {
|
||||
this.reconnectFuture = getTaskScheduler().schedule(() -> {
|
||||
try {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Attempting reconnect");
|
||||
}
|
||||
logger.debug("Attempting reconnect");
|
||||
synchronized (MqttPahoMessageDrivenChannelAdapter.this) {
|
||||
if (!MqttPahoMessageDrivenChannelAdapter.this.connected) {
|
||||
connectAndSubscribe();
|
||||
@@ -367,21 +362,21 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (MqttException e) {
|
||||
logger.error("Exception while connecting and subscribing", e);
|
||||
catch (MqttException ex) {
|
||||
logger.error(ex, "Exception while connecting and subscribing");
|
||||
scheduleReconnect();
|
||||
}
|
||||
}, new Date(System.currentTimeMillis() + this.recoveryInterval));
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error("Failed to schedule reconnect", e);
|
||||
catch (Exception ex) {
|
||||
logger.error(ex, "Failed to schedule reconnect");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void connectionLost(Throwable cause) {
|
||||
if (isRunning()) {
|
||||
this.logger.error("Lost connection: " + cause.getMessage() + "; retrying...");
|
||||
this.logger.error(() -> "Lost connection: " + cause.getMessage() + "; retrying...");
|
||||
this.connected = false;
|
||||
if (this.client != null) {
|
||||
try {
|
||||
@@ -411,9 +406,9 @@ public class MqttPahoMessageDrivenChannelAdapter extends AbstractMqttMessageDriv
|
||||
try {
|
||||
sendMessage(message);
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
logger.error("Unhandled exception for " + message.toString(), e);
|
||||
throw e;
|
||||
catch (RuntimeException ex) {
|
||||
logger.error(ex, () -> "Unhandled exception for " + message.toString());
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -167,7 +167,7 @@ public class MqttPahoMessageHandler extends AbstractMqttMessageHandler
|
||||
String url = getUrl();
|
||||
if (url != null) {
|
||||
options = MqttUtils.cloneConnectOptions(options);
|
||||
options.setServerURIs(new String[] { url });
|
||||
options.setServerURIs(new String[]{ url });
|
||||
}
|
||||
}
|
||||
return options;
|
||||
@@ -194,8 +194,8 @@ public class MqttPahoMessageHandler extends AbstractMqttMessageHandler
|
||||
this.client = null;
|
||||
}
|
||||
}
|
||||
catch (MqttException e) {
|
||||
logger.error("Failed to disconnect", e);
|
||||
catch (MqttException ex) {
|
||||
logger.error(ex, "Failed to disconnect");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,11 +45,11 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.eclipse.paho.client.mqttv3.IMqttAsyncClient;
|
||||
import org.eclipse.paho.client.mqttv3.IMqttClient;
|
||||
@@ -64,7 +64,8 @@ import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.eclipse.paho.client.mqttv3.MqttToken;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentMatchers;
|
||||
import org.mockito.internal.stubbing.answers.CallsRealMethods;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactoryBean;
|
||||
@@ -74,6 +75,7 @@ import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.log.LogAccessor;
|
||||
import org.springframework.integration.StaticMessageHeaderAccessor;
|
||||
import org.springframework.integration.channel.NullChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
@@ -403,7 +405,7 @@ public class MqttAdapterTests {
|
||||
final IMqttClient client = mock(IMqttClient.class);
|
||||
MqttPahoMessageDrivenChannelAdapter adapter = buildAdapterIn(client, null, ConsumerStopAction.UNSUBSCRIBE_NEVER);
|
||||
adapter.setRecoveryInterval(10);
|
||||
Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
|
||||
LogAccessor logger = spy(TestUtils.getPropertyValue(adapter, "logger", LogAccessor.class));
|
||||
new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
|
||||
given(logger.isDebugEnabled()).willReturn(true);
|
||||
final AtomicInteger attemptingReconnectCount = new AtomicInteger();
|
||||
@@ -461,7 +463,7 @@ public class MqttAdapterTests {
|
||||
willReturn(alwaysComplete).given(aClient).connect(any(MqttConnectOptions.class), any(), any());
|
||||
|
||||
IMqttToken token = mock(IMqttToken.class);
|
||||
given(token.getGrantedQos()).willReturn(new int[] { 0x80 });
|
||||
given(token.getGrantedQos()).willReturn(new int[]{ 0x80 });
|
||||
willReturn(token).given(aClient).subscribe(any(String[].class), any(int[].class), isNull(), isNull(), any());
|
||||
|
||||
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("foo", "bar", factory,
|
||||
@@ -473,11 +475,11 @@ public class MqttAdapterTests {
|
||||
}, m -> m.getName().equals("connectAndSubscribe"));
|
||||
assertThat(method.get()).isNotNull();
|
||||
Condition<InvocationTargetException> subscribeFailed = new Condition<>(ex ->
|
||||
((MqttException) ex.getCause()).getReasonCode() == MqttException.REASON_CODE_SUBSCRIBE_FAILED,
|
||||
"expected the reason code to be REASON_CODE_SUBSCRIBE_FAILED");
|
||||
((MqttException) ex.getCause()).getReasonCode() == MqttException.REASON_CODE_SUBSCRIBE_FAILED,
|
||||
"expected the reason code to be REASON_CODE_SUBSCRIBE_FAILED");
|
||||
assertThatExceptionOfType(InvocationTargetException.class).isThrownBy(() -> method.get().invoke(adapter))
|
||||
.withCauseInstanceOf(MqttException.class)
|
||||
.is(subscribeFailed);
|
||||
.withCauseInstanceOf(MqttException.class)
|
||||
.is(subscribeFailed);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -510,7 +512,7 @@ public class MqttAdapterTests {
|
||||
willReturn(alwaysComplete).given(aClient).connect(any(MqttConnectOptions.class), any(), any());
|
||||
|
||||
IMqttToken token = mock(IMqttToken.class);
|
||||
given(token.getGrantedQos()).willReturn(new int[] { 2, 0 });
|
||||
given(token.getGrantedQos()).willReturn(new int[]{ 2, 0 });
|
||||
willReturn(token).given(aClient).subscribe(any(String[].class), any(int[].class), isNull(), isNull(), any());
|
||||
|
||||
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("foo", "bar", factory,
|
||||
@@ -521,12 +523,15 @@ public class MqttAdapterTests {
|
||||
method.set(m);
|
||||
}, m -> m.getName().equals("connectAndSubscribe"));
|
||||
assertThat(method.get()).isNotNull();
|
||||
Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
|
||||
LogAccessor logger = spy(TestUtils.getPropertyValue(adapter, "logger", LogAccessor.class));
|
||||
new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
|
||||
given(logger.isWarnEnabled()).willReturn(true);
|
||||
method.get().invoke(adapter);
|
||||
verify(logger, atLeastOnce())
|
||||
.warn("Granted QOS different to Requested QOS; topics: [baz, fix] requested: [1, 1] granted: [2, 0]");
|
||||
.warn(ArgumentMatchers.<Supplier<? extends CharSequence>>argThat(logMessage ->
|
||||
logMessage.get()
|
||||
.equals("Granted QOS different to Requested QOS; topics: [baz, fix] " +
|
||||
"requested: [1, 1] granted: [2, 0]")));
|
||||
verify(client).setTimeToWait(30_000L);
|
||||
|
||||
new DirectFieldAccessor(adapter).setPropertyValue("running", Boolean.TRUE);
|
||||
@@ -545,7 +550,7 @@ public class MqttAdapterTests {
|
||||
|
||||
};
|
||||
MqttConnectOptions connectOptions = new MqttConnectOptions();
|
||||
connectOptions.setServerURIs(new String[] { "tcp://localhost:1883" });
|
||||
connectOptions.setServerURIs(new String[]{ "tcp://localhost:1883" });
|
||||
if (cleanSession != null) {
|
||||
connectOptions.setCleanSession(cleanSession);
|
||||
}
|
||||
@@ -572,7 +577,7 @@ public class MqttAdapterTests {
|
||||
|
||||
};
|
||||
MqttConnectOptions connectOptions = new MqttConnectOptions();
|
||||
connectOptions.setServerURIs(new String[] { "tcp://localhost:1883" });
|
||||
connectOptions.setServerURIs(new String[]{ "tcp://localhost:1883" });
|
||||
factory.setConnectionOptions(connectOptions);
|
||||
MqttPahoMessageHandler adapter = new MqttPahoMessageHandler("client", factory);
|
||||
adapter.setDefaultTopic("foo");
|
||||
|
||||
Reference in New Issue
Block a user