-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add capability to determine topic names #11
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/uk/gov/justice/framework/tools/command/ListTopics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package uk.gov.justice.framework.tools.command; | ||
|
||
import uk.gov.justice.framework.tools.common.command.ShellCommand; | ||
|
||
public class ListTopics extends AbstractArtemisCommand implements ShellCommand { | ||
|
||
@Override | ||
public void run(String[] args) { | ||
|
||
try { | ||
final String[] queues = artemisConnector.topicNames(host, port, brokerName); | ||
outputPrinter.writeStringArray(queues); | ||
} catch (final Exception exception) { | ||
outputPrinter.writeStackTrace(exception); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
package uk.gov.justice.artemis.manager.util; | ||
|
||
import static org.apache.activemq.artemis.api.jms.ActiveMQJMSClient.createQueue; | ||
import static org.apache.activemq.artemis.api.jms.ActiveMQJMSClient.createTopic; | ||
|
||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.jms.Connection; | ||
import javax.jms.ConnectionFactory; | ||
import javax.jms.JMSException; | ||
import javax.jms.Message; | ||
import javax.jms.MessageConsumer; | ||
import javax.jms.MessageProducer; | ||
import javax.jms.Queue; | ||
import javax.jms.QueueConnection; | ||
import javax.jms.QueueConnectionFactory; | ||
import javax.jms.QueueSession; | ||
import javax.jms.Session; | ||
import javax.jms.TextMessage; | ||
import javax.jms.Topic; | ||
import javax.jms.TopicSubscriber; | ||
|
||
import org.apache.activemq.artemis.jms.client.ActiveMQQueueConnectionFactory; | ||
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory; | ||
|
||
public class JmsTestUtil { | ||
|
||
private static final QueueConnectionFactory JMS_CF = new ActiveMQQueueConnectionFactory("tcp://localhost:61616"); | ||
private static QueueConnection JMS_CONNECTION; | ||
private static QueueSession JMS_SESSION; | ||
private static final ConnectionFactory JMS_CF = new ActiveMQConnectionFactory("tcp://localhost:61616?clientID=1234"); | ||
private static Connection JMS_CONNECTION; | ||
private static Session JMS_SESSION; | ||
private static Map<String, Queue> QUEUES = new HashMap<>(); | ||
private static Map<String, MessageConsumer> CONSUMERS = new HashMap<>(); | ||
private static Map<String, MessageProducer> PRODUCERS = new HashMap<>(); | ||
private static Map<String, Topic> TOPICS = new HashMap<>(); | ||
private static Map<String, TopicSubscriber> SUBSCRIBERS = new HashMap<>(); | ||
private static Map<String, MessageProducer> PUBLISHERS = new HashMap<>(); | ||
|
||
public static void putInQueue(final String queueName, final String msgText, final String... origAddress) throws JMSException { | ||
TextMessage message = JMS_SESSION.createTextMessage(msgText); | ||
|
@@ -48,11 +52,19 @@ public static void putInQueue(final String queueName, final InputStream messageI | |
producerOf(queueName).send(message); | ||
} | ||
|
||
public static void putOnTopic(final String topicName, final String msgText, final String... origAddress) throws JMSException { | ||
TextMessage message = JMS_SESSION.createTextMessage(msgText); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final |
||
if (origAddress.length > 0) { | ||
message.setStringProperty("_AMQ_ORIG_ADDRESS", origAddress[0]); | ||
} | ||
publisherOf(topicName).send(message); | ||
} | ||
|
||
/** | ||
* Returns the number of messages that were removed from the queue. | ||
* | ||
* @param queueName - the name of the queue that is to be cleaned | ||
* @return the number of cleaned messagesß | ||
* @return the number of cleaned messages | ||
*/ | ||
public static int cleanQueue(final String queueName) throws JMSException { | ||
JMS_CONNECTION.start(); | ||
|
@@ -66,6 +78,24 @@ public static int cleanQueue(final String queueName) throws JMSException { | |
return cleanedMessage; | ||
} | ||
|
||
/** | ||
* Returns the number of messages that were received from the topic. | ||
* | ||
* @param topicName - the name of the topic that is to be cleaned | ||
* @return the number of cleaned messages | ||
*/ | ||
public static int cleanTopic(final String topicName, final String name) throws JMSException { | ||
JMS_CONNECTION.start(); | ||
final TopicSubscriber subscriber = subscriberOf(topicName, name); | ||
|
||
int cleanedMessage = 0; | ||
while (subscriber.receiveNoWait() != null) { | ||
cleanedMessage++; | ||
} | ||
JMS_CONNECTION.stop(); | ||
return cleanedMessage; | ||
} | ||
|
||
public static MessageConsumer consumerOf(final String queueName) throws JMSException { | ||
return CONSUMERS.computeIfAbsent(queueName, name -> { | ||
try { | ||
|
@@ -86,23 +116,50 @@ private static MessageProducer producerOf(final String queueName) throws JMSExce | |
}); | ||
} | ||
|
||
private static MessageProducer publisherOf(final String topicName) throws JMSException { | ||
return PUBLISHERS.computeIfAbsent(topicName, name -> { | ||
try { | ||
return JMS_SESSION.createProducer(topicOf(name)); | ||
} catch (JMSException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
private static TopicSubscriber subscriberOf(final String topicName, final String subscriptionName) throws JMSException { | ||
return SUBSCRIBERS.computeIfAbsent(topicName, name -> { | ||
try { | ||
return JMS_SESSION.createDurableSubscriber(topicOf(name), subscriptionName); | ||
} catch (JMSException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
|
||
public static void closeJmsConnection() throws JMSException { | ||
SUBSCRIBERS.clear(); | ||
PUBLISHERS.clear(); | ||
|
||
CONSUMERS.clear(); | ||
PRODUCERS.clear(); | ||
|
||
TOPICS.clear(); | ||
QUEUES.clear(); | ||
|
||
JMS_CONNECTION.close(); | ||
} | ||
|
||
public static void openJmsConnection() throws JMSException { | ||
JMS_CONNECTION = JMS_CF.createQueueConnection(); | ||
JMS_SESSION = JMS_CONNECTION.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); | ||
|
||
JMS_CF.createConnection(); | ||
JMS_CONNECTION = JMS_CF.createConnection(); | ||
JMS_SESSION = JMS_CONNECTION.createSession(false, Session.AUTO_ACKNOWLEDGE); | ||
} | ||
|
||
private static Queue queueOf(final String queueName) { | ||
return QUEUES.computeIfAbsent(queueName, name -> createQueue(queueName)); | ||
} | ||
|
||
|
||
private static Topic topicOf(final String topicName) { | ||
return TOPICS.computeIfAbsent(topicName, name -> createTopic(topicName)); | ||
} | ||
} |
6 changes: 2 additions & 4 deletions
6
src/test/java/uk/gov/justice/framework/tools/command/BrowseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/java/uk/gov/justice/framework/tools/command/ListTopicsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package uk.gov.justice.framework.tools.command; | ||
|
||
import static org.mockito.Matchers.anyString; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
import uk.gov.justice.artemis.manager.connector.ArtemisConnector; | ||
|
||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ListTopicsTest { | ||
|
||
@Mock | ||
ArtemisConnector artemisConnector; | ||
|
||
@InjectMocks | ||
ListTopics listTopicsCommand; | ||
|
||
@Test | ||
public void shouldInvokeConnector() throws Exception { | ||
listTopicsCommand.brokerName = "brokerabc"; | ||
listTopicsCommand.host = "some.host"; | ||
listTopicsCommand.port = "1212"; | ||
when(artemisConnector.topicNames(anyString(), anyString(), anyString())).thenReturn(new String[] {"testTopic" }); | ||
|
||
listTopicsCommand.run(null); | ||
verify(artemisConnector).topicNames("some.host", "1212", "brokerabc"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define and throw a dedicated exception instead of using a generic one.