eclipse 搭建连接 activemq
今天我特地写下笔记,希望可以完全掌握这个东西,也希望可以帮助到任何想对学习这个东西的同学。
1.下载activemq压缩包,并解压(如果需要下载请看文章尾部附录)
2.进入bin文件夹,(64位电脑就进入win64,,32进入win32)找到activemq.bat 文件,双击运行,产生如下界面
3.访问activemq页面http://localhost:8161,用户名密码都是admin
4.然后点击队列或者主题,本项目以主题为例
5.创建一个主题
可以看到默认都是0
6.打开eclipse,并新建maven项目
7.添加需要的jar包
8.添加tomcat服务
9.先运行测试,测试是否成功
7.运行成功后,开始正式写连接activemq代码
创建class
Producer.java代码如下:
1 package chansea.ToConnActivemq; 2 3 import java.util.Arrays; 4 import java.util.Random; 5 import java.util.Timer; 6 import java.util.TimerTask; 7 8 import javax.jms.Connection; 9 import javax.jms.ConnectionFactory; 10 import javax.jms.Destination; 11 import javax.jms.JMSException; 12 import javax.jms.MessageProducer; 13 import javax.jms.ObjectMessage; 14 import javax.jms.Session; 15 import javax.jms.TextMessage; 16 17 import org.apache.activemq.ActiveMQConnection; 18 import org.apache.activemq.ActiveMQConnectionFactory; 19 20 import net.sf.json.JSONObject; 21 22 import utils.Utils; 23 24 public class ProducerStu { 25 26 // 默认连接用户名 27 private static final String USERNAME = ActiveMQConnection.DEFAULT_USER; 28 // 默认连接密码 29 private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD; 30 // 默认连接地址 31 private static final String BROKERURL = ActiveMQConnection.DEFAULT_BROKER_URL; 32 // 发送的消息数量 33 private static final int SENDNUM = 1; 34 35 public static void main(String[] args) { 36 // 连接工厂 37 ConnectionFactory connectionFactory; 38 // 连接 39 Connection connection = null; 40 // 会话,接收或者发送消息的线程 41 Session session; 42 // 消息的目的地 43 Destination destination; 44 // 消息生产者 45 MessageProducer messageProducer; 46 // 实例化连接工厂 47 connectionFactory = new ActiveMQConnectionFactory(ProducerStu.USERNAME, ProducerStu.PASSWORD, ProducerStu.BROKERURL); 48 49 try { 50 // 通过连接工厂获取连接 51 connection = connectionFactory.createConnection(); 52 // 启动连接 53 connection.start(); 54 // 创建session 55 session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE); 56 // 创建一个名称为Status_Iot_Machines_J6-3_J6-3-9的消息队列 57 destination = session.createTopic("Status_Iot_Machines_J6-3_J6-3-9"); 58 // 创建消息生产者 59 messageProducer = session.createProducer(destination); 60 // 发送消息 61 sendMessage(session, messageProducer); 62 63 } catch (Exception e) { 64 e.printStackTrace(); 65 } 66 67 } 68 69 /** 70 * 发送消息 71 * 72 * @param session 链接会话 73 * @param messageProducer 消息生产者 74 * @throws Exception 75 */ 76 public static void sendMessage(final Session session, final MessageProducer messageProducer) throws Exception { 77 final Utils u = new Utils(); 78 System.out.println("----start:" + u.intToDateTime(System.currentTimeMillis(), "-")); 79 TimerTask task = new TimerTask() { 80 @Override 81 public void run() { 82 try { 83 Random r = new Random(); 84 int[] status = {-1,0,1,3,4,13,14,15,16,17}; 85 int _statusIndex = r.nextInt(status.length); 86 87 String[] macCode = {"N01","N02","N03","N04","N05","N06","N07","N08","N09","N10","N11","N12","N13","N14","N15","N16","N17","N18","N19","N20","N21","N22","N23","N24","N25","N26","N27","N28","N29","N30","N31","N32","N33","N34","N35","N36","N37","N38","N39","N40","N41","N42","N43","N44","N45","N46","N47","N48","N49","N50","N51","N52","N53","N54","N55","N56","N57","N58","N59","N60","N61","N62","N63","N64","N65","N66","N67","N68","N69","N70","N71","N72","N73","N74","N75","N76","N77","N78","N79","N80","N81","N82","N83","N84","N85","N86","N87","N88","N89","N90","N91","N92","N93","N94","N95","N96","N97","N98","N99","N100","N101","N102","N103","N104","N105","N106","N107","N108","N109","N110","N111","N112","N113","N114","N115","N116","N117","N118","N119","N120","N121","N122","N123","N124","N125","N126","N127","N128","N129","N130","N131","N132","N133","N134","N135","N136","N137","N138","N139","N140","N141","N142","N143","N144","N145","N146","N147","N148","N149","N150","N151","N152","N153","N154","N155","N156","N157","N158","N159","N160","N161","N162","N163","N164","N165","N166","N167","N168","N169","N170","N171","N172","N173","N174","N175","N176","N177","N178","N179","N180","N181","N182","N183","N184","N185","N186","N187","N188","N189","N190","N191","N192","N193","N194","N195","N196","N197","N198","N199","N200","N201","N202","N203","N204","N205","N206","N207","N208","N209","N210","N211","N212","N213","N214","N215","N216","N217","N218","N219","N220","N221","N222","N223","N224","N225","N226","N227","N228","N229","N230","N231","N232","N233","N234","N235","N236","N237","N238","N239","N240","N241","N242","N243","N244","N245","N246","N248","N249","N250","N251","N253","N254","N255","N256","N257","N258","N259","N260","N261","N262","N263","N264","N265","N266","N267","N268","N269","N270","N271","N272","N273","N274","N275","N276","N278","N279","N280","N281","N282","N283","N284","N285","N286","N287","N288","N289","N290","N291","N292","N293","N294","N295","N296","N297","N298","N299","N300","N301","N302","N303","N304","N305","N306","N307","N308","N309","N311","N312","N313","N314","N315","N316","N317","N318","N319","N320","N321","N322","N323","N324","N325","N326","N327","N328","N329","N330","N331","N332","N333","N334","N338","N340","N341","N342","N343","N344","N345","N346","N347","N348","N349","N350","N351","N352","N353","N354","N355","N356","N357","N358","N359","N360","N361","N362","N363","N364","N365","N366","N367","N368","N369","N371","N374","N375","N376","N377","N378","N379","N380","N381","N382","N383","N384","N385","N386","N387","N388","N389","N390","N391","N392","N393","N394","N395","N396","N397","N398","N399","N400","N401","N402","N403","N404","N405","N406","N407","N408","N409","N410","N411","N412","N413","N414","N415","N416","N417","N418","N419","N420","N421","N422","N423","N424","N425","N426","N427","N429","N430","N431","N432","N433","N434","N435","N436","N437","N440","N441","N442","N443","N444","N445","N446","N447","N448","N449","N450","N451","N452","N453","N454","N455","N456","N457","N458","N459","N460","N461","N462","N463","N464","N465","N466","N467","N468","N469","N470","N471","N472","N473","N474","N475","N476","N477","N478","N480","N481","N482","N483","N484","N485","N486","N487","N488","N489","N490","N491","N492","N493","N520","N521","N522","N524","N525","N526","N527","N528","N529","N550","N551","N552","N553","N554","N555","N556","N557","N558","N559","N560","N561","N562","N563","N564","N565","N566","N567","N568","N569","N570","N571","N572","N573","N574","N575","N576","N577","N578","N579","N580","N581","N582","N583","N584","N585","N586","N587","N588","N589","N590","N591","N592","N593","N594","N595","N596","N597","N598","N599","N600","N601","N602","N603","N604","N605","N606","N607","N608","N609","N610","N611","N612","N613","N614","N615","N616","N617","N618","N619","N620","N621","N622","N623","N624","N625","N626","N627","N628","N629","N630","N631","N632","N633","N634","N635","N636","N637","N638","N639","N640","N641","N642","N643","N644","N645","N646","N647","N648","N649","N650","N651","N652","N653","N654","N655","N656","N657","N658","N659","N660","N661","N662","N663","N664","N665","N666","N667","N668","N669","N670","N671","N672","N673","N674","N675","N676","N677","N678","N679","N680","N681","N682","N683","N684","N685","N686","N687","N688","N689","N690","N691","N692","N693","N694","N695","N696","N697","N698","N699","N700","N701","N702","N703","N704","N705","N706","N707","N708","N709","N710","N711","N712","N713","N714","N715","N716","N717","N718","N719","N720","N721","N722","N723","N724","N725","N726","N727","N728","N729","N730","N731","N732","N733","N734","N735","N736","N737","N738","N739","N740","N741","N742","N743","N744","N745","N746","N747","N748","N749","N750","N751","N752","N753","N754","N755","N756","N757","N758","N759","N760","N761","N762","N763","N764","N765","N766","N767","N768","N769","N770","N771","N772","N773","N774","N775","N776","N777","N778","N779","N780","N781","N782","N783","N784","N785","N786","N787","N788","N789","N790","N791","N792","N793","N794","N795","N796","N797","N798","N799","N800","N801","N802","N803","N804","N805","N806","N807","N808","N809","N810","N811","N812","N813","N814","N815","N816","N817","N818","N819","N820","N821","N822","N823","N824","N825","N826","N827","N828","N829","N830","N831","N832","N833","N834","N835","N836","N837","N838","N839","N840","N841","N842","N843","N844","N845","N846","N847","N848","N849","N850","N851","N852","N853","N854","N855","N856","N857","N858","N859","N860","N861","N862","N863","N864","N865","N866","N867","N868","N869","N870","N871","N872","N873","N874","N875","N876","N877","N878","N879","N880","N881","N882","N883","N884","N885","N886","N887","N888","N889","N890","N891","N892","N893","N894","N895","N896","N897","N898","N899","N900","N901","N902","N903","N904","N905","N906","N907","N908","N909","N910","N911","N912","N913","N914","N915","N916","N917","N918","N919","N920","N921","N922","N923","N924","N925","N926","N927","N928","N929","N930","N931","N932","N933","N934","N935","N936","N937","N938","N939","N940","N941","N942","N943","N944","N945","N946","N947","N948","N949","N950","N951","N952","N953","N954","N955","N956","N957","N958","N959","N960","N961","N962","N963","N964","N965","N966","N967","N968","N969","N970","N971","N972","N973","N974","N975","N976","N977","N978","N979","N980"}; 88 int _index = r.nextInt(macCode.length - 1); 89 90 JSONObject obj = new JSONObject(); 91 obj.element("MachineCode", macCode[_index]); 92 obj.element("RunMode_Cur", "MEMory"); 93 obj.element("StatusID_Cur", status[_statusIndex]); 94 obj.element("StatusID_Pre", status[_statusIndex]); 95 obj.element("StatusStep", r.nextInt(1000)); 96 obj.element("Type_Data", 13); 97 obj.element("pub_time", System.currentTimeMillis()); 98 System.out.println("消息:" + obj + u.intToDateTime(obj.getLong("pub_time"), "-")); 99 // 创建一条文本消息 100 TextMessage message = session.createTextMessage(obj.toString()); 101 // 通过消息生产者发出消息 102 messageProducer.send(message); 103 session.commit(); 104 } catch (JMSException e) { 105 // TODO Auto-generated catch block 106 e.printStackTrace(); 107 } 108 } 109 }; 110 Timer timer = new Timer(); 111 // 开始等待时间 112 long delay = 100; 113 // 定义每次执行的间隔时间 114 long intevalPeriod = 1 * 900; 115 // 安排任务在一段时间内运行 116 timer.scheduleAtFixedRate(task, delay, intevalPeriod); 117 } 118 119 }
utils.java代码如下(本例为打印需要,如果不需要可不必添加,只需在Producer代码上相关utils的代码注释即可):
package utils; import java.text.SimpleDateFormat; import java.util.Date; public class Utils { public String intToDateTime(Long timestamp, String symbol){ String sd = ""; Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss"); sd = sdf.format(new Date(timestamp)); // 时间戳转换日期 return sd; } }
运行后
10.接着去查看activemq的也页面生产者是否大于0
有大于0,就算成功了
附录1:activemq下载
链接:https://pan.baidu.com/s/1jW0221hRnQ0kgV_XuOF-eg
提取码:9z6c
附录2:eclipse下载
链接:https://pan.baidu.com/s/1obT7Owr4G9VVs5o2rZkc0g
提取码:891y
附录3:jdk下载
链接:https://pan.baidu.com/s/1dsiMxYLQnBFQMOraU1ZTyw
提取码:yo23
附录4:tomcat下载
链接:https://pan.baidu.com/s/1Iob2hBFoT1LC3H3Y5XVJGA
提取码:rpfu
附录5:需要的jar包
https://files.cnblogs.com/files/Chansea/eclipse搭建连接activemq所需JAR包.zip