Skip to content

Commit 6f5c73b

Browse files
committed
1 parent d0438d5 commit 6f5c73b

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

common/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<properties>
1515
<maven.compiler.source>11</maven.compiler.source>
1616
<maven.compiler.target>11</maven.compiler.target>
17+
18+
<activemq.version>5.14.5</activemq.version>
1719
</properties>
1820

1921
<dependencies>
@@ -33,6 +35,12 @@
3335
<artifactId>guice</artifactId>
3436
<version>5.1.0</version>
3537
</dependency>
38+
39+
<dependency>
40+
<groupId>org.apache.activemq</groupId>
41+
<artifactId>activemq-broker</artifactId>
42+
<version>${activemq.version}</version>
43+
</dependency>
3644
</dependencies>
3745

3846
<build>
@@ -53,6 +61,46 @@
5361
</annotationProcessorPaths>
5462
</configuration>
5563
</plugin>
64+
65+
<!--
66+
Starts a minimal embedded ActiveMQ broker for the test.
67+
-->
68+
<plugin>
69+
<groupId>org.apache.activemq.tooling</groupId>
70+
<artifactId>activemq-maven-plugin</artifactId>
71+
<version>${activemq.version}</version>
72+
<configuration>
73+
<configUri>xbean:file:${project.build.testOutputDirectory}/activemq.xml</configUri>
74+
<fork>true</fork>
75+
<systemProperties>
76+
<property>
77+
<name>org.apache.activemq.default.directory.prefix</name>
78+
<value>./target/</value>
79+
</property>
80+
</systemProperties>
81+
</configuration>
82+
<dependencies>
83+
<dependency>
84+
<groupId>org.apache.activemq</groupId>
85+
<artifactId>activemq-spring</artifactId>
86+
<version>${activemq.version}</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.apache.activemq</groupId>
90+
<artifactId>activemq-leveldb-store</artifactId>
91+
<version>${activemq.version}</version>
92+
</dependency>
93+
</dependencies>
94+
<executions>
95+
<execution>
96+
<id>start-activemq</id>
97+
<goals>
98+
<goal>run</goal>
99+
</goals>
100+
<phase>process-test-resources</phase>
101+
</execution>
102+
</executions>
103+
</plugin>
56104
</plugins>
57105
</build>
58106
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package by.andd3dfx.jms;
2+
3+
import org.apache.activemq.broker.jmx.BrokerViewMBean;
4+
import org.apache.activemq.broker.jmx.QueueViewMBean;
5+
6+
import javax.management.MBeanServerConnection;
7+
import javax.management.MBeanServerInvocationHandler;
8+
import javax.management.MalformedObjectNameException;
9+
import javax.management.ObjectName;
10+
import javax.management.remote.JMXConnector;
11+
import javax.management.remote.JMXConnectorFactory;
12+
import javax.management.remote.JMXServiceURL;
13+
import java.io.IOException;
14+
15+
/**
16+
* To make JMX available for ActiveMQ it should be configured in config activemq.xml:
17+
* <managementContext>
18+
* <managementContext createConnector="true"/>
19+
* </managementContext>
20+
*/
21+
public class JmsUtil {
22+
23+
public static Long getQueueSize(String activeMqUrl, String queueName) throws Exception {
24+
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(activeMqUrl));
25+
MBeanServerConnection connection = connector.getMBeanServerConnection();
26+
27+
ObjectName objectName = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost");
28+
BrokerViewMBean brokerViewMBean;
29+
brokerViewMBean = MBeanServerInvocationHandler.newProxyInstance(connection, objectName, BrokerViewMBean.class, true);
30+
brokerViewMBean.addQueue(queueName);
31+
32+
for (ObjectName name : brokerViewMBean.getQueues()) {
33+
QueueViewMBean queueViewMBean = MBeanServerInvocationHandler.newProxyInstance(connection, name, QueueViewMBean.class, true);
34+
35+
if (queueViewMBean != null && queueViewMBean.getName().equals(queueName)) {
36+
return queueViewMBean.getQueueSize();
37+
}
38+
}
39+
return null;
40+
}
41+
42+
public static Long getQueueSize2(String activeMqUrl, String queueName) throws IOException, MalformedObjectNameException {
43+
JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(activeMqUrl));
44+
MBeanServerConnection connection = connector.getMBeanServerConnection();
45+
46+
ObjectName objectName = new ObjectName("org.apache.activemq:type=Broker,brokerName=localhost,destinationType=Queue,destinationName=" + queueName);
47+
QueueViewMBean queueViewMBean = MBeanServerInvocationHandler.newProxyInstance(connection, objectName, QueueViewMBean.class, true);
48+
49+
if (queueViewMBean != null) {
50+
return queueViewMBean.getQueueSize();
51+
}
52+
53+
return null;
54+
}
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package by.andd3dfx.jms;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.MatcherAssert.assertThat;
6+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
7+
8+
/**
9+
* To perform check - start Active MQ broker with JMX enabled in configuration according to notes in JmsUtil
10+
*/
11+
public class JmsUtilTest {
12+
private final String ACTIVEMQ_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi";
13+
private final String QUEUE_NAME = "JMS/DGST.MIGRATE.IN";
14+
15+
@Test
16+
public void getQueueSize() throws Exception {
17+
Long queueSize = JmsUtil.getQueueSize(ACTIVEMQ_URL, QUEUE_NAME);
18+
19+
assertThat("Size should be >=0", queueSize, greaterThanOrEqualTo(0L));
20+
}
21+
22+
@Test
23+
public void getQueueSize2() throws Exception {
24+
Long queueSize = JmsUtil.getQueueSize2(ACTIVEMQ_URL, QUEUE_NAME);
25+
26+
assertThat("Size should be >=0", queueSize, greaterThanOrEqualTo(0L));
27+
}
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<beans
2+
xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5+
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
6+
7+
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="ActiveMQData" useShutdownHook="false">
8+
<persistenceAdapter>
9+
<kahaDB directory="target/ActiveMQData/kahadb"/>
10+
</persistenceAdapter>
11+
12+
<transportConnectors>
13+
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=100"/>
14+
</transportConnectors>
15+
16+
<managementContext>
17+
<managementContext createConnector="true"/>
18+
</managementContext>
19+
</broker>
20+
</beans>

0 commit comments

Comments
 (0)