Skip to content

Commit

Permalink
Add MessageProducerClientBuilder for creating configurable message pr…
Browse files Browse the repository at this point in the history
…oducer clients
  • Loading branch information
allanmckenzie committed Aug 30, 2022
1 parent 6b86dd2 commit 38732d0
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Added MessageProducerClientBuilder for creating configurable message producer clients

## [11.0.0-M18] - 2022-08-12
### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ public class MessageProducerClient implements AutoCloseable {
private MessageProducer messageProducer;
private Connection connection;

private ActiveMQConnectionFactory activeMQConnectionFactory;

@Deprecated(since = "Please use MessageProducerClientBuilder to create instead of using this constructor")
public MessageProducerClient() {
this(new ActiveMQConnectionFactory());
}

MessageProducerClient(final ActiveMQConnectionFactory activeMQConnectionFactory) {
this.activeMQConnectionFactory = activeMQConnectionFactory;
}

/**
* Starts the message producer for a specific topic. Must be called
* before any messages can be sent.
Expand All @@ -37,8 +48,8 @@ public class MessageProducerClient implements AutoCloseable {
public void startProducer(final String topicName) {

try {
final ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(QUEUE_URI);
connection = factory.createConnection();
activeMQConnectionFactory.setBrokerURL(QUEUE_URI);
connection = activeMQConnectionFactory.createConnection();
connection.start();

session = connection.createSession(false, AUTO_ACKNOWLEDGE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package uk.gov.justice.services.test.utils.core.messaging;

import static java.util.Optional.empty;
import static java.util.Optional.of;

import java.util.Optional;

import com.google.common.annotations.VisibleForTesting;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;

public class MessageProducerClientBuilder {

private Optional<Integer> retryInterval = empty();
private Optional<Integer> maxRetryInterval = empty();
private Optional<Double> retryIntervalMultiplier = empty();
private Optional<Integer> reconnectAttempts = empty();

private final ActiveMQConnectionFactory activeMQConnectionFactory;

public static MessageProducerClientBuilder aMessageProducerClient() {
return new MessageProducerClientBuilder(new ActiveMQConnectionFactory());
}

@VisibleForTesting
private MessageProducerClientBuilder(final ActiveMQConnectionFactory activeMQConnectionFactory) {
this.activeMQConnectionFactory = activeMQConnectionFactory;
}

public MessageProducerClientBuilder withRetryInterval(final int retryInterval) {
this.retryInterval = of(retryInterval);
return this;
}

public MessageProducerClientBuilder withMaxRetryInterval(final int maxRetryInterval) {
this.maxRetryInterval = of(maxRetryInterval);
return this;
}

public MessageProducerClientBuilder withRetryIntervalMultiplier(final double retryIntervalMultiplier) {
this.retryIntervalMultiplier = of(retryIntervalMultiplier);
return this;
}

public MessageProducerClientBuilder withReconnectAttempts(int reconnectAttempts) {
this.reconnectAttempts = of(reconnectAttempts);
return this;
}

public MessageProducerClient build() {

retryInterval.ifPresent(activeMQConnectionFactory::setRetryInterval);
maxRetryInterval.ifPresent(activeMQConnectionFactory::setMaxRetryInterval);
retryIntervalMultiplier.ifPresent(activeMQConnectionFactory::setRetryIntervalMultiplier);
reconnectAttempts.ifPresent(activeMQConnectionFactory::setReconnectAttempts);

return new MessageProducerClient(activeMQConnectionFactory);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package uk.gov.justice.services.test.utils.core.messaging;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static uk.gov.justice.services.test.utils.core.messaging.MessageProducerClientBuilder.aMessageProducerClient;
import static uk.gov.justice.services.test.utils.core.reflection.ReflectionUtil.getValueOfField;

import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MessageProducerClientBuilderTest {

@Mock
private ActiveMQConnectionFactory activeMQConnectionFactory;

@InjectMocks
private MessageProducerClientBuilder messageProducerClientBuilder;

@Test
public void shouldHaveStaticFactoryMethod() throws Exception {
assertThat(aMessageProducerClient(), is(notNullValue()));
}

@Test
public void shouldCreateMessageConsumerClientWithNoExtraActiveMQConnectionFactoryProperties() throws Exception {

final MessageProducerClient messageProducerClient = messageProducerClientBuilder.build();
assertThat(messageProducerClient, is(notNullValue()));

verifyZeroInteractions(activeMQConnectionFactory);

final ActiveMQConnectionFactory theActiveMQConnectionFactory = getValueOfField(messageProducerClient, "activeMQConnectionFactory", ActiveMQConnectionFactory.class);


assertThat(theActiveMQConnectionFactory, is(sameInstance(activeMQConnectionFactory)));
}

@Test
public void shouldSetRetryIntervalOnActiveMQConnectionFactory() throws Exception {

final int retryInterval = 23;

final MessageProducerClient messageProducerClient = messageProducerClientBuilder
.withRetryInterval(retryInterval)
.build();
assertThat(messageProducerClient, is(notNullValue()));

verify(activeMQConnectionFactory).setRetryInterval(retryInterval);
verifyNoMoreInteractions(activeMQConnectionFactory);
}

@Test
public void shouldSetMaxRetryIntervalOnActiveMQConnectionFactory() throws Exception {

final int maxRetryInterval = 23;

final MessageProducerClient messageProducerClient = messageProducerClientBuilder
.withMaxRetryInterval(maxRetryInterval)
.build();
assertThat(messageProducerClient, is(notNullValue()));

verify(activeMQConnectionFactory).setMaxRetryInterval(maxRetryInterval);
verifyNoMoreInteractions(activeMQConnectionFactory);
}

@Test
public void shouldSetMaxRetryIntervalMultiplierOnActiveMQConnectionFactory() throws Exception {

final double retryIntervalMultiplier = 23.1;

final MessageProducerClient messageProducerClient = messageProducerClientBuilder
.withRetryIntervalMultiplier(retryIntervalMultiplier)
.build();
assertThat(messageProducerClient, is(notNullValue()));

verify(activeMQConnectionFactory).setRetryIntervalMultiplier(retryIntervalMultiplier);
verifyNoMoreInteractions(activeMQConnectionFactory);
}

@Test
public void shouldSetReconnectAttemptsOnActiveMQConnectionFactory() throws Exception {

final int reconnectAttempts = 23;

final MessageProducerClient messageProducerClient = messageProducerClientBuilder
.withReconnectAttempts(reconnectAttempts)
.build();
assertThat(messageProducerClient, is(notNullValue()));

verify(activeMQConnectionFactory).setReconnectAttempts(reconnectAttempts);
verifyNoMoreInteractions(activeMQConnectionFactory);
}
}

0 comments on commit 38732d0

Please sign in to comment.