定制网站开发接私活,企业公众号,各大网站官网的导航栏怎么做,wordPress登不上数据库1. 安装 ActiveMQ
1.1 下载 ActiveMQ
访问 ActiveMQ 官方下载页面#xff0c;根据你的操作系统选择合适的版本进行下载。这里以 Linux 系统#xff0c;Java环境1.8版本为例#xff0c;下载 apache-activemq-5.16.7-bin.tar.gz。
1.2 解压文件
将下载的压缩包解压到指定目…1. 安装 ActiveMQ
1.1 下载 ActiveMQ
访问 ActiveMQ 官方下载页面根据你的操作系统选择合适的版本进行下载。这里以 Linux 系统Java环境1.8版本为例下载 apache-activemq-5.16.7-bin.tar.gz。
1.2 解压文件
将下载的压缩包解压到指定目录例如 /opt
tar -zxvf apache-activemq-5.16.7-bin.tar.gz -C /opt1.3 启动 ActiveMQ
进入解压后的目录启动 ActiveMQ
cd /opt/apache-activemq-5.16.7
./bin/activemq start1.4 验证 ActiveMQ 是否启动成功
打开浏览器访问 http://localhost:8161使用默认用户名 admin 和密码 admin 登录 ActiveMQ 的管理控制台。如果能成功登录说明 ActiveMQ 已经启动成功。
1.4 进入ActiveMQ 管理员控制台
ActiveMQ 启动成功后单击 Manage ActiveMQ broker 超链接进入管理员控制台。
2. 创建 Spring Boot 项目并整合 JMS - ActiveMQ
2.1 添加依赖
在 pom.xml 中添加 Spring Boot 集成 ActiveMQ 的依赖
dependencies!-- Spring Boot Web --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- Spring Boot JMS --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-activemq/artifactId/dependency
/dependencies2.2 配置 ActiveMQ
在 application.properties 中配置 ActiveMQ 的连接信息
server.port8080
# ActiveMQ 服务器地址默认端口 61616
spring.activemq.broker-urltcp://localhost:61616
# 配置信任所有的包这个配置是为了支持发送对象消息
spring.activemq.packages.trust-alltrue
# ActiveMQ 用户名
spring.activemq.useradmin
# ActiveMQ 密码
spring.activemq.passwordadmin
2.3 创建消息生产者
创建一个消息生产者类用于发送消息到 ActiveMQ 的队列
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;import javax.jms.Queue;Service
public class JmsProducer {Autowiredprivate JmsTemplate jmsTemplate;Autowiredprivate Queue queue;public void sendMessage(String message) {jmsTemplate.convertAndSend(queue, message);System.out.println(Sent message: message);}
}2.4 创建消息消费者
创建一个消息消费者类用于接收 ActiveMQ 队列中的消息
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;Component
public class JmsConsumer {JmsListener(destination test-queue)public void receiveMessage(String message) {System.out.println(Received message: message);}
}
2.5 配置 Queue Bean
在 ActiveMqConfig.java 中定义 队列
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.jms.Queue;Configuration
public class ActiveMqConfig {Beanpublic Queue queue() {return new ActiveMQQueue(test-queue);}
}
2.6 创建 API 测试发送消息
import com.weigang.producer.JmsProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;RestController
RequestMapping(/message)
public class MessageController {Autowiredprivate JmsProducer producer;GetMapping(/send)public String sendMessage(RequestParam String msg) {producer.sendMessage(msg);return Message sent: msg;}
}
然后访问http://localhost:8080/message/send?msgHelloActiveMQ
控制台应输出
Sent message: HelloActiveMQ
Received message: HelloActiveMQ3. 使用 ActiveMQ Web 界面查看消息
访问 http://localhost:8161/admin/queues.jsp可以看到 test-queue 队列以及发送的消息。
4. 发送对象消息
在 JmsProducer 发送 对象
import com.weigang.model.CustomMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;import javax.jms.Queue;Service
public class JmsProducer {Autowiredprivate JmsTemplate jmsTemplate;// 仍然使用 test-queueAutowiredprivate Queue queue;public void sendMessage(CustomMessage customMessage) {jmsTemplate.convertAndSend(queue, customMessage);System.out.println(Sent message---- id customMessage.getId() ,content customMessage.getContent());}
}
创建消息对象
import java.io.Serializable;public class CustomMessage implements Serializable {private static final long serialVersionUID 1L; // 推荐添加避免序列化问题private String content;private int id;// 必须有默认构造方法JMS 反序列化需要public CustomMessage() {}// 构造方法public CustomMessage(String content, int id) {this.content content;this.id id;}public String getContent() {return content;}public void setContent(String content) {this.content content;}public int getId() {return id;}public void setId(int id) {this.id id;}Overridepublic String toString() {return CustomMessage{ content content \ , id id };}}
消费者处理对象消息
import com.weigang.model.CustomMessage;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;Component
public class JmsConsumer {JmsListener(destination test-queue)public void receiveMessage(String message) {System.out.println(Received message: message);}JmsListener(destination test-queue)public void receiveMessage(CustomMessage message) {System.out.println(Received object message: message.toString());}
}通过 API 发送对象
import com.weigang.model.CustomMessage;
import com.weigang.producer.JmsProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;RestController
RequestMapping(/message)
public class MessageController {Autowiredprivate JmsProducer producer;GetMapping(/send)public String sendMessage(RequestParam String msg) {producer.sendMessage(msg);return Message sent: msg;}GetMapping(/sendObj)public String sendMessage(RequestParam Integer id,RequestParam String content) {CustomMessage customMessage new CustomMessage(content, id);producer.sendMessage(customMessage);return Message sent: customMessage;}
}然后访问http://localhost:8080/message/sendObj?id1contentHelloActiveMQ
控制台应输出
Sent message---- id1,contentHelloActiveMQ
Received object message: CustomMessage{contentHelloActiveMQ, id1}注意事项
确保 ActiveMQ 服务器正常运行并且 application.properties 中的连接信息正确。如果需要使用主题Topic进行消息传递可以在配置中设置 spring.jms.pub-sub-domaintrue并相应地修改消息生产者和消费者的代码。 文章转载自: http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.mrqwy.cn.gov.cn.mrqwy.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.jjhng.cn.gov.cn.jjhng.cn http://www.morning.jokesm.com.gov.cn.jokesm.com http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.jlnlr.cn.gov.cn.jlnlr.cn http://www.morning.jpkhn.cn.gov.cn.jpkhn.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.yggwn.cn.gov.cn.yggwn.cn http://www.morning.zzbwjy.cn.gov.cn.zzbwjy.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.smnxr.cn.gov.cn.smnxr.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.fbfnk.cn.gov.cn.fbfnk.cn http://www.morning.ghrhb.cn.gov.cn.ghrhb.cn http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn http://www.morning.saastob.com.gov.cn.saastob.com http://www.morning.hkng.cn.gov.cn.hkng.cn http://www.morning.hgtr.cn.gov.cn.hgtr.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.sbczr.cn.gov.cn.sbczr.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.mzbyl.cn.gov.cn.mzbyl.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.nxrgl.cn.gov.cn.nxrgl.cn http://www.morning.wjfzp.cn.gov.cn.wjfzp.cn http://www.morning.hrzhg.cn.gov.cn.hrzhg.cn http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.tbwsl.cn.gov.cn.tbwsl.cn http://www.morning.yuminfo.com.gov.cn.yuminfo.com http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.tbknh.cn.gov.cn.tbknh.cn http://www.morning.spsqr.cn.gov.cn.spsqr.cn http://www.morning.fhhry.cn.gov.cn.fhhry.cn http://www.morning.dnvhfh.cn.gov.cn.dnvhfh.cn http://www.morning.glwyn.cn.gov.cn.glwyn.cn http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn http://www.morning.dygsz.cn.gov.cn.dygsz.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.kxltf.cn.gov.cn.kxltf.cn http://www.morning.pwrkl.cn.gov.cn.pwrkl.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.lprfk.cn.gov.cn.lprfk.cn http://www.morning.ktsth.cn.gov.cn.ktsth.cn http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.dkfb.cn.gov.cn.dkfb.cn http://www.morning.kfcz.cn.gov.cn.kfcz.cn http://www.morning.yqwsd.cn.gov.cn.yqwsd.cn http://www.morning.fhqdb.cn.gov.cn.fhqdb.cn http://www.morning.fplwz.cn.gov.cn.fplwz.cn http://www.morning.wnywk.cn.gov.cn.wnywk.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.hqllj.cn.gov.cn.hqllj.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.rtsx.cn.gov.cn.rtsx.cn http://www.morning.xlmgq.cn.gov.cn.xlmgq.cn http://www.morning.wfspn.cn.gov.cn.wfspn.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.pzpj.cn.gov.cn.pzpj.cn http://www.morning.sgjw.cn.gov.cn.sgjw.cn http://www.morning.kgfsz.cn.gov.cn.kgfsz.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.ybqlb.cn.gov.cn.ybqlb.cn