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 Producer {
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(Producer.USERNAME, Producer.PASSWORD, Producer.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 String[] macCode = {"N260","N692","N77","N697","N123","N232","N651","N213","N972","N290","N954","N633","N632","N196","N378","N81","N921","N489","N150","N791","N555","N713","N963","N666","N240","N563","N617","N825","N243","N275","N815","N49","N377","N244","N215","N689","N250","N587","N837","N268","N465","N96","N132","N398","N803","N811","N55","N16","N646","N777","N225","N412","N79","N441","N672","N457","N466","N447","N293","N190","N93","N361","N643","N430","N404","N819","N889","N771","N286","N844","N05","N550","N848","N890","N647","N596","N690","N974","N425","N09","N321","N662","N344","N941","N54","N957","N612","N166","N214","N121","N748","N842","N977","N807","N488","N316","N11","N616","N202","N939","N384","N834","N882","N135","N80","N357","N681","N14","N418","N806","N938","N222","N393","N122","N980","N362","N226","N381","N161","N434","N374","N01","N579","N925","N851","N267","N456","N956","N635","N206","N08","N390","N769","N452","N136","N172","N60","N348","N353","N212","N294","N831","N29","N451","N231","N426","N840","N710","N127","N449","N603","N915","N369","N259","N936","N375","N352","N895","N554","N208","N912","N445","N141","N839","N695","N269","N433","N106","N88","N475","N604","N432","N664","N714","N891","N755","N471","N600","N949","N906","N759","N356","N745","N284","N879","N111","N333","N712","N239","N733","N278","N21","N758","N210","N299","N216","N405","N169","N109","N648","N875","N953","N932","N382","N61","N707","N860","N894","N06","N273","N238","N854","N856","N933","N143","N562","N946","N301","N679","N350","N642","N797","N251","N944","N801","N25","N112","N283","N892","N351","N589","N608","N871","N861","N696","N200","N47","N300","N904","N709","N847","N31","N762","N964","N796","N327","N786","N526","N154","N732","N620","N778","N195","N182","N836","N360","N116","N102","N824","N151","N249","N282","N482","N104","N07","N832","N853","N553","N33","N660","N287","N607","N152","N417","N421","N905","N193","N774","N58","N459","N454","N950","N221","N125","N812","N551","N867","N62","N48","N265","N217","N930","N237","N419","N766","N305","N28","N621","N82","N354","N593","N472","N168","N756","N270","N38","N552","N970","N723","N328","N920","N817","N558","N297","N828","N264","N570","N685","N924","N627","N574","N649","N407","N194","N900","N147","N585","N380","N975","N852","N850","N70","N17","N741","N493","N827","N885","N669","N580","N435","N785","N823","N559","N188","N913","N317","N784","N708","N98","N227","N798","N958","N520","N177","N18","N303","N883","N789","N24","N720","N207","N976","N754","N73","N440","N897","N862","N728","N341","N50","N638","N880","N91","N255","N704","N443","N295","N485","N192","N849","N181","N57","N66","N137","N594","N198","N702","N156","N63","N306","N173","N951","N887","N59","N878","N37","N203","N257","N961","N365","N575","N808","N923","N622","N703","N582","N611","N829","N826","N470","N74","N205","N597","N654","N464","N326","N869","N945","N877","N962","N338","N967","N258","N911","N474","N311","N414","N857","N782","N148","N211","N176","N253","N423","N687","N899","N809","N914","N387","N820","N734","N581","N866","N522","N947","N323","N10","N779","N749","N367","N266","N160","N446","N484","N845","N795","N876","N557","N640","N722","N140","N315","N15","N235","N673","N567","N392","N800","N246","N624","N197","N422","N319","N650","N164","N100","N04","N560","N910","N726","N602","N814","N525","N670","N615","N307","N92","N750","N960","N35","N613","N163","N658","N929","N19","N671","N588","N746","N67","N870","N65","N02","N979","N340","N677","N684","N262","N973","N864","N481","N03","N868","N400","N737","N978","N916","N655","N129","N599","N117","N409","N201","N224","N721","N285","N399","N566","N322","N320","N12","N917","N609","N199","N189","N155","N881","N928","N395","N406","N934","N142","N170","N483","N183","N281","N572","N966","N52","N408","N72","N427","N680","N688","N776","N431","N56","N349","N292","N174","N491","N686","N388","N731","N898","N462","N312","N765","N639","N955","N22","N394","N128","N379","N436","N114","N893","N943","N735","N661","N610","N347","N873","N27","N276","N358","N790","N578","N302","N584","N165","N682","N799","N478","N32","N455","N346","N942","N230","N770","N242","N469","N97","N644","N623","N810","N444","N144","N653","N467","N75","N131","N590","N442","N813","N68","N220","N781","N901","N126","N524","N792","N752","N569","N725","N940","N656","N751","N36","N740","N413","N576","N179","N233","N20","N926","N30","N636","N256","N23","N363","N701","N209","N107","N528","N397","N931","N527","N178","N71","N401","N788","N793","N747","N437","N601","N371","N94","N715","N149","N678","N772","N775","N314","N113","N261","N219","N332","N157","N204","N331","N411","N463","N835","N450","N959","N838","N368","N729","N101","N85","N396","N663","N968","N234","N110","N468","N40","N822","N487","N761","N458","N922","N325","N816","N298","N919","N606","N167","N652","N705","N228","N191","N383","N896","N78","N794","N229","N476","N521","N764","N185","N355","N416","N146","N739","N918","N171","N716","N742","N162","N90","N486","N184","N366","N279","N45","N313","N763","N460","N490","N659","N153","N330","N41","N342","N115","N665","N477","N805","N969","N473","N865","N821","N296","N694","N420","N69","N236","N46","N711","N908","N591","N691","N13","N843","N403","N773","N134","N492","N846","N886","N952","N935","N907","N263","N571","N308","N410","N391","N625","N657","N461","N241","N86","N667","N158","N818","N902","N359","N345","N573","N389","N26","N619","N744","N700","N274","N927","N271","N304","N874","N105","N291","N187","N783","N223","N718","N618","N364","N124","N568","N787","N424","N448","N592","N668","N43","N95","N53","N727","N757","N888","N736","N334","N120","N39","N318","N595","N605","N626","N83","N309","N44","N186","N699","N34","N133","N598","N965","N561","N385","N329","N564","N280","N180","N937","N218","N84","N376","N415","N630","N738","N863","N675","N64","N706","N556","N175","N159","N683","N139","N99","N324","N138","N130","N254","N629","N245","N767","N641","N802","N89","N717","N453","N833","N289","N903","N288","N948","N753","N628","N108","N119","N429","N343","N859","N804","N841","N76","N884","N577","N272","N103","N693","N87","N724","N698","N480","N872","N743","N971","N529","N118","N780","N42","N631","N645","N768","N730","N402","N614","N719","N586","N637","N583","N858","N145","N760","N565","N674","N909","N676","N855","N386","N248","N830","N51","N634"};
87 int len = macCode.length - 1;
88 int _index = (int) (Math.random() * len);
89 while (_index > macCode.length - 1) {
90 if (_index <= macCode.length - 1) {
91 break;
92 }
93 _index = (int) (Math.random() * len);
94 }
95
96 JSONObject obj = new JSONObject();
97 obj.element("MachineCode", macCode[_index]);
98 obj.element("RunMode_Cur", "MEMory");
99 obj.element("StatusID_Cur", status[_statusIndex]);
100 obj.element("StatusID_Pre", status[_statusIndex]);
101 obj.element("StatusStep", 23);
102 obj.element("Type_Data", 13);
103 obj.element("pub_time", System.currentTimeMillis());
104 System.out.println("消息:" + obj + u.intToDateTime(obj.getLong("pub_time"), "-"));
105 // 创建一条文本消息
106 TextMessage message = session.createTextMessage(obj.toString());
107 // 通过消息生产者发出消息
108 messageProducer.send(message);
109 session.commit();
110 } catch (JMSException e) {
111 // TODO Auto-generated catch block
112 e.printStackTrace();
113 }
114 }
115 };
116 Timer timer = new Timer();
117 // 开始等待时间
118 long delay = 100;
119 // 定义每次执行的间隔时间
120 long intevalPeriod = 5 * 10;
121 // 安排任务在一段时间内运行
122 timer.scheduleAtFixedRate(task, delay, intevalPeriod);
123 }
124
125 }
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");
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
eclipse 搭建连接 activemq的更多相关文章
- paip.最好的脚本语言node js 环境搭建连接mysql
paip.最好的脚本语言node js 环境搭建连接mysql #====下载node...走十一个exe..容易的.. 1 #0----Hello world .js 2 #---------模 ...
- 利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)
前言 本文旨在利用maven搭建ssm环境,而关于maven的具体内容,大家可以去阅读<Maven 实战>.其实园内这方面文章已有不少,那么为什么我还要重复造轮子呢?我只是想记录自己的实践 ...
- hadoop开发环境部署——通过eclipse远程连接hadoop2.7.3进行开发
一.前言 环境: 系统:centos6.5 hadoop版本:Apache hadoop2.7.3(Windows和centos都是同一个) eclipse版本:4.2.0(juno版本,window ...
- windows下eclipse远程连接hadoop集群开发mapreduce
转载请注明出处,谢谢 2017-10-22 17:14:09 之前都是用python开发maprduce程序的,今天试了在windows下通过eclipse java开发,在开发前先搭建开发环境.在 ...
- Eclipse搭建Maven Prooject(终于)
今天我们一起来探讨eclipse搭建maven项目的步骤 一.建一个maven项目, 1.找到File ,点击 2.弹出窗口,再点击new 3.弹出窗口,点击other 4.输入maven,找到mav ...
- Eclipse搭建c环境(CDT)二
Eclipse 编辑c程序环境的搭建主要为其安装CDT插件即可 Eclipse搭建CDT步骤如下: 1.首先配置好java环境,为后续运行eclipse做准备 (略) 2.下载并安装eclipse(这 ...
- windows、ubuntu下eclipse搭建java、Python环境问题总结
前两篇博文分别讲述了如何在windows.ubuntu下用eclipse搭建java.python环境,下面就针对本人遇到的问题做一个总结. 一.windows下关于java环境变量JAVA_HOME ...
- ubuntu上用eclipse搭建java、python开发环境
上一篇文章讲到如何在windwos上用eclipse搭建java.python开发环境,这一讲将关注如何在ubuntu上实现搭建,本人使用虚拟机安装的ubuntu系统,系统版本为:14.04 lts ...
- windows 下用eclipse搭建java、python开发环境
本人只针对小白!本文只针对小白!本文只针对小白! 最近闲来无事,加上之前虽没有做过eclipse上java.python的开发工作,但一直想尝试一下.于是边查找资料边试验,花了一天时间在自己的机器上用 ...
随机推荐
- IO复用之poll
主要用一个例程来讲解poll,包含客户端和服务器端. poll函数没有FD_SETSIZE的限制 int poll(struct pollfd * fdarray, unsigned long nfd ...
- pandas.DataFarme内置的绘图功能参数说明
可视化是数据探索性分析及结果表达的一种非常重要的形式,因此打算写一个python绘图系列,本文是第一篇,先说一下pandas.DataFrame.plot()绘图功能. pandas.DataFram ...
- 解决自动安装Freebsd系统盘符无法确定问题
最近因为需要用到Freebsd,所以研究了打包的一些方法,这个没什么太大问题,通过网上的一些资料可以解决,但是由于确实不太熟悉这套系统,还是碰上了一些比较麻烦的地方,目前也没看到有人写如何处理,那就自 ...
- 深入理解h2和r2dbc-h2
简介 本文将会介绍R2DBC的H2实现r2dbc-h2的使用方法和要注意的事项.一起来看看吧. H2数据库简介 什么是H2数据库呢? H2是一个Java SQL database,它是一个开源的数据库 ...
- nacos服务注册源码解析
1.客户端使用 compile 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:2.2.3.RELEASE' compi ...
- elementui checkbox复选框实现层级联动
使用elementui 实现复选框的层级联动,可能我的表述不准确,先上一个效果图. 实际开发中可能遇到这样的场景,当选择高一层级的复选框时它包含的低级的复选框就不需要再勾选,需要默认选中并且禁止选用. ...
- java面试必问:多线程的实现和同步机制,一文帮你搞定多线程编程
前言 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进程的地址空间是互相隔离的:进程拥有各种资源和状态信息,包括打开的文件.子进程和信号处理. 线 ...
- On Starting My Blogging On CNBlogs
About Myself I am a would-be Master of Engineering, on the last year of my student life, I'm ambitou ...
- [CF1454] Codeforces Round #686 (Div. 3) solution
标签(空格分隔): 经验 题解 时量 : 2h 概括 : \[\text{2min t1 }\\ \text{10min t2 (hacked)}\\ \text{30min t3 }\\ \text ...
- pip递归安装报错继续安装
使用命令pip freeze > requirements.txt导出项目依赖包之后,使用pip -r requirements.txt的时候可能会出现一些包安装出错,但是安装一旦出错,安装就会 ...