activeMQ安装使用

怼烎@ 2022-08-26 12:29 247阅读 0赞

下载地址:

http://mirror.esocc.com/apache/activemq/apache-activemq/5.9.0/apache-activemq-5.9.0-bin.tar.gz

拷贝到安装路径。例如:

/usr/local/activeMQ/

解压

tar -xvf apache-activemq-5.9.0-bin.tar.gz

启动activeMQ服务

cd bin/

根据当前系统环境执行使用相应的文件进行启动服务即可。

例如:当前是Linux X86 64位操作系统

cd linux-x86-64

./activemq start

可选择option

{ console | start | stop | restart | status | dump }

启动完成后,就可以通过activeMQ自带的后台管理了。

后台地址

http://localhost:8161/hawtio/#

用户名:admin

密码:admin

接下来我们使用跑一个Active自带的事例

Publisher 实现发布

Listener 订阅

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package example;
  18. import java.text.DateFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. import javax.jms.Connection;
  22. import javax.jms.DeliveryMode;
  23. import javax.jms.Destination;
  24. import javax.jms.JMSException;
  25. import javax.jms.MessageProducer;
  26. import javax.jms.Session;
  27. import javax.jms.TextMessage;
  28. import org.apache.activemq.ActiveMQConnectionFactory;
  29. import org.apache.activemq.command.ActiveMQTopic;
  30. class Publisher extends Thread {
  31. static String user = env("ACTIVEMQ_USER", "admin");
  32. static String password = env("ACTIVEMQ_PASSWORD", "password");
  33. static String host = env("ACTIVEMQ_HOST", "10.1.15.123");
  34. static int port = Integer.parseInt(env("ACTIVEMQ_PORT", "61616"));
  35. @Override
  36. public void run() {
  37. try {
  38. String destination = "event_lss";
  39. int messages = 10000;
  40. ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://" + host + ":" + port);
  41. Connection connection = factory.createConnection(user, password);
  42. connection.start();
  43. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  44. Destination dest = new ActiveMQTopic(destination);
  45. MessageProducer producer = session.createProducer(dest);
  46. // 非持久化模式
  47. producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
  48. for (int i = 1; i <= messages; i++) {
  49. String body = getSendMsg(i);
  50. TextMessage msg = session.createTextMessage(body);
  51. msg.setIntProperty("id", i);
  52. producer.send(msg);
  53. if ((i % 10) == 0) {
  54. System.out.println(String.format("Sent %d messages", i));
  55. try {
  56. Thread.sleep(100);
  57. } catch (InterruptedException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. }
  62. producer.send(session.createTextMessage("SHUTDOWN"));
  63. connection.close();
  64. } catch (JMSException ex) {
  65. ex.printStackTrace();
  66. }
  67. }
  68. public static void main(String[] args) throws JMSException {
  69. for (int i = 0; i < 10; i++) {
  70. new Publisher().start();
  71. }
  72. }
  73. private String getSendMsg(int i) {
  74. DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,ms");
  75. return getName() + "\t" + i + "\t" + format.format(new Date()) + "abcdefghijklmnopqrstuvwxyz";
  76. }
  77. private static String env(String key, String defaultValue) {
  78. String rc = System.getenv(key);
  79. if (rc == null)
  80. return defaultValue;
  81. return rc;
  82. }
  83. private static String arg(String[] args, int index, String defaultValue) {
  84. if (index < args.length)
  85. return args[index];
  86. else
  87. return defaultValue;
  88. }
  89. }
  90. /**
  91. * Licensed to the Apache Software Foundation (ASF) under one or more
  92. * contributor license agreements. See the NOTICE file distributed with
  93. * this work for additional information regarding copyright ownership.
  94. * The ASF licenses this file to You under the Apache License, Version 2.0
  95. * (the "License"); you may not use this file except in compliance with
  96. * the License. You may obtain a copy of the License at
  97. *
  98. * http://www.apache.org/licenses/LICENSE-2.0
  99. *
  100. * Unless required by applicable law or agreed to in writing, software
  101. * distributed under the License is distributed on an "AS IS" BASIS,
  102. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  103. * See the License for the specific language governing permissions and
  104. * limitations under the License.
  105. */
  106. package example;
  107. import org.apache.activemq.ActiveMQConnectionFactory;
  108. import org.apache.activemq.command.ActiveMQTopic;
  109. import javax.jms.*;
  110. class Listener {
  111. public static void main(String []args) throws JMSException {
  112. String user = env("ACTIVEMQ_USER", "admin");
  113. String password = env("ACTIVEMQ_PASSWORD", "password");
  114. String host = env("ACTIVEMQ_HOST", "10.1.15.123");
  115. int port = Integer.parseInt(env("ACTIVEMQ_PORT", "61616"));
  116. String destination = arg(args, 0, "event_lss");
  117. ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://" + host + ":" + port);
  118. Connection connection = factory.createConnection(user, password);
  119. connection.start();
  120. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  121. Destination dest = new ActiveMQTopic(destination);
  122. MessageConsumer consumer = session.createConsumer(dest);
  123. long start = System.currentTimeMillis();
  124. long count = 1;
  125. System.out.println("Waiting for messages...");
  126. while(true) {
  127. Message msg = consumer.receive();
  128. if( msg instanceof TextMessage ) {
  129. String body = ((TextMessage) msg).getText();
  130. if( "SHUTDOWN".equals(body)) {
  131. long diff = System.currentTimeMillis() - start;
  132. System.out.println(String.format("Received %d in %.2f seconds", count, (1.0*diff/1000.0)));
  133. break;
  134. } else {
  135. // if( count != msg.getIntProperty("id") ) {
  136. // System.out.println("mismatch: "+count+"!="+msg.getIntProperty("id"));
  137. // }
  138. count = msg.getIntProperty("id");
  139. if( count == 0 ) {
  140. start = System.currentTimeMillis();
  141. }
  142. if( count % 1000 == 0 ) {
  143. System.out.println(String.format("Received %d messages.", count));
  144. }
  145. System.out.println(body);
  146. count ++;
  147. }
  148. } else {
  149. System.out.println("Unexpected message type: "+msg.getClass());
  150. }
  151. }
  152. connection.close();
  153. }
  154. private static String env(String key, String defaultValue) {
  155. String rc = System.getenv(key);
  156. if( rc== null )
  157. return defaultValue;
  158. return rc;
  159. }
  160. private static String arg(String []args, int index, String defaultValue) {
  161. if( index < args.length )
  162. return args[index];
  163. else
  164. return defaultValue;
  165. }
  166. }

发表评论

表情:
评论列表 (有 0 条评论,247人围观)

还没有评论,来说两句吧...

相关阅读