文章目录
- 一、ActiveMQ环境搭建
- 1.1、控制台环境搭建
- 1.2、控制台配置
- 二、SpringBoot开发环境搭建
- 三、JMeter脚本二次开发环境搭建
一、ActiveMQ环境搭建
1.1、控制台环境搭建
下载 ActiveMQ 地址为:
https://activemq.apache.org/components/classic/download/
注意:
拷贝apache-activemq-6.12-bin.tar.gz到Linux服务器的/opt下
解压缩 tar -zxf apache-activemq-6.12-bin.tar.gz
重命名 mv apache-activemq-6.12 activemq
进入目录后显示:
[root@lw activemq]# ll
总用量 11148
-rwxr-xr-x. 1 root root 11354996 4月 12 01:35 activemq-all-6.1.2.jar
drwxr-xr-x. 4 root root 130 7月 16 14:28 bin
drwxr-xr-x. 2 root root 4096 7月 16 14:28 conf
drwxr-xr-x. 2 root root 26 7月 16 14:28 data
drwxr-xr-x. 2 root root 76 7月 16 14:28 docs
drwxr-xr-x. 7 root root 71 4月 12 01:35 examples
drwxr-xr-x. 6 root root 4096 7月 16 14:29 lib
-rw-r--r--. 1 root root 40581 4月 12 01:35 LICENSE
-rw-r--r--. 1 root root 3334 4月 12 01:35 NOTICE
-rw-r--r--. 1 root root 2611 4月 12 01:35 README.txt
drwxr-xr-x. 6 root root 95 7月 16 14:28 webapps
drwxr-xr-x. 3 root root 18 7月 16 14:28 webapps-demo
配置JDK路径:
[root@lw activemq]# vim /opt/activemq/bin/activemq
# 添加java_home/java_cmd环境配置如:
启动服务命令 service activemq start
:
[root@lw ~]# service activemq start
INFO: Loading '/opt/activemq//bin/setenv'
INFO: Using java '/usr/local/jdk-17.0.11/bin/java'
INFO: Starting - inspect logfiles specified in logging.properties and log4j2.properties to get details
INFO: pidfile created : '/opt/activemq//data/activemq.pid' (pid '117820')
[root@lw ~]#
打开浏览器验证是否可以打开ActiveMQ控制台:
1.2、控制台配置
修改配置文件 /opt/activemq/conf/jetty.xml
:
修发现8161对应的ip地址是127.0.0.1,这个地址一般都是localhost的地址,所以需要把这个地址改成0.0.0.0,即广播地址。
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start"><!-- the default port number for the web console --><property name="host" value="0.0.0.0"/><property name="port" value="8161"/></bean><bean id="Server" depends-on="jettyPort" class="org.eclipse.jetty.server.Server"destroy-method="stop"><property name="handler"><bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerCollection"><property name="handlers"><list><ref bean="contexts" /><ref bean="securityHandler" /></list></property></bean></property>
重新启动,打开浏览器地址:
二、SpringBoot开发环境搭建
新建 maven下项目并且在 pom 文件中输入:
<dependencies><!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-pool --><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-pool</artifactId><version>${activemq.version}</version></dependency>
</dependencies>
上面步骤完成后,就可以新建一个成产者进行测试是否可以用队列生产者搭建,代码参考如下:
public static void main(String[] args) throws JMSException {String brokerURL = "tcp://192.168.24.137:61616";//构造连接工厂实例对象ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURL);//构造从工厂得到连接对象Connection connection = connectionFactory.createConnection();//启动connection.start();//获取操作连接Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//获取session,注意指定消息队列Queue queue = session.createQueue("7dGroup-test");//消息的目的地,消息发给谁MessageProducer producer = session.createProducer(queue);//消息发送者//构造消息,此处写死,项目就是参数,或者方法获取TextMessage message = session.createTextMessage("我是测试者");producer.send(message);producer.close();session.close();connection.close();}
打开网址点击 Queues :
执行代码提示成功:
控制台显示:
点击 Topics 显示如下:
如果修改 Topics 发送消息,代码修改一个地方即可,如下图:
参考代码如下:
Topic topic = session.createTopic("7dGroup-yace");//消息的目的地,消息发给谁MessageProducer producer = session.createProducer(topic);//消息发送者//构造消息,此处写死,项目就是参数,或者方法获取TextMessage message = session.createTextMessage("我是测试者");
执行代码查看控制台:
在查看 Queues没有增加数据:
消费者搭建,参考代码:
String brokerURL = "tcp://192.168.24.137:61616";//构造连接工厂实例对象ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURL);//构造从工厂得到连接对象Connection connection = connectionFactory.createConnection();//启动connection.start();//获取操作连接Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//获取session,注意指定消息队列Queue queue = session.createQueue("7dGroup-test");// 创建消费者MessageConsumer consumer = session.createConsumer(queue);while (true) {TextMessage message = (TextMessage) consumer.receive();if (null != message) {System.out.println("我收到一条消息" + message.getText());} elsebreak;}consumer.close();session.close();connection.close();}
执行结果显示:
查看控制台目前已经有一个消费者:
如果采用监听器方式进行消费参考代码如下:
String brokerURL = "tcp://192.168.24.137:61616";//构造连接工厂实例对象ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURL);//构造从工厂得到连接对象Connection connection = connectionFactory.createConnection();//启动connection.start();//获取操作连接Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//获取session,注意指定消息队列Queue queue = session.createQueue("7dGroup-test");// 创建消费者MessageConsumer consumer = session.createConsumer(queue);consumer.setMessageListener(new MessageListener() {@Overridepublic void onMessage(Message message) {if (null != message && message instanceof TextMessage) {TextMessage textMessage = (TextMessage) message;try {System.out.println("我收到一条消息: " + textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}}});try {//会一直执行获取数据System.in.read();consumer.close();session.close();connection.close();} catch (IOException e) {throw new RuntimeException(e);}}
再次运行生产者,监听器会自动获取数据:
执行两次生产,执行一次监听器:
通过上面代码已经把 activemq 环境搭建好,现在开始修改代码为Jmeter 脚本,在修改jmeter脚本之前,需要引入 ApacheJMeter_functions 方法才能使用java 协议进行脚本开发。
<!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_functions --><dependency><groupId>org.apache.jmeter</groupId><artifactId>ApacheJMeter_functions</artifactId><version>${jmeter.version}</version></dependency>
三、JMeter脚本二次开发环境搭建
在代码中新建一个 ActiveMQSampler 类并且继承 AbstractJavaSamplerClient 接口参考如下:
public class ActiveMQSamplerTest extends AbstractJavaSamplerClient {private String brokerURL;private String topic;private String message;// 判断是topic还是队列消息private String isTopMess;@Overridepublic void setupTest(JavaSamplerContext context) {brokerURL = context.getParameter("brokerURL");topic = context.getParameter("topic");message = context.getParameter("message");isTopMess = context.getParameter("isTopMess");}@Overridepublic SampleResult runTest(JavaSamplerContext sampleResult) {// 创建 Jmeter 请求结果SampleResult result = new SampleResult();try {// 创建消息Producer msg = new Producer();msg.SedProducerTopMess(brokerURL, topic, message, isTopMess);// 请求开始result.sampleStart();result.sampleEnd();result.setSuccessful(true);} catch (Exception e) {//e.printStackTrace();result.sampleEnd();result.setSuccessful(false);}return result;}@Overridepublic Arguments getDefaultParameters() {Arguments params = new Arguments();params.addArgument("brokerURL", "tcp://localhost:61616");params.addArgument("topic", "testTopic");params.addArgument("message", "testTag");params.addArgument("isTopMess", "queue");return params;}
}
生成消息改造后的代码,如下:
/*** 是否发送 topic 与队列的区别** @param brokerURL* @param topicStr* @param messageStr* @param isTopMess*/public void SedProducerTopMess(String brokerURL, String topicStr, String messageStr, String isTopMess) {try {//构造连接工厂实例对象ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURL);//构造从工厂得到连接对象Connection connection = connectionFactory.createConnection();//启动connection.start();//获取操作连接Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);//获取session,注意指定消息队列MessageProducer producer;if (isTopMess.equals("queue")) {Queue queue = session.createQueue(topicStr);producer = session.createProducer(queue);} else {Topic topic = session.createTopic(topicStr);//消息的目的地,消息发给谁producer = session.createProducer(topic);}logger.info("目前发送主题为:" + topicStr + ",的消息为:" + messageStr);//消息发送者//构造消息,此处写死,项目就是参数,或者方法获取TextMessage message = session.createTextMessage(messageStr);producer.send(message);producer.close();session.close();connection.close();} catch (JMSException e) {throw new RuntimeException(e);}}
接下来把代码编译成 jar 放入 jmeter 中的 lib/ext下,如果想把依赖都一起打成 jar,请参考pom文件如下:
<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>17</source><target>17</target><encoding>UTF-8</encoding></configuration></plugin><!-- 依赖包--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>2.6</version><configuration><encoding>utf-8</encoding><resources><resource><directory>${basedir}/src/main/resources</directory><includes><include>**/*.xml</include><include>**/*.config</include></includes></resource></resources></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-assembly-plugin</artifactId><configuration><archive><manifest><mainClass>io.pef.mq</mainClass></manifest></archive><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build>
编程后如:
复制包,如下图:
接下来打开 JMeter 验证脚本是否可以用?
修改地址,进行发送消息测试:
开启监听器消费,需要修改当前 JMeter发送的主题如下:
再次执行 JMeter 进行生存消息,代码执行消息如下:
结果如下:
再次打开控制台如:
到这里脚本已经全部开发完毕,希望对大家有一点帮助。