ActiveMQ+Spring2.5(转)

本文详细介绍了如何使用ActiveMQ与Spring框架进行集成,实现消息的发送与接收。通过配置JMS连接工厂、JMS模板及消息队列,展示了发送与接收JMS消息的Java代码实例,并通过Spring配置文件进行整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ActiveMQ+Spring2.5

ActiveMQJMSmavenSpringBean.


项目环境:

JDK1.5

ActiveMQ5.2

POM文件




Xml代码 
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
3.  <modelVersion>4.0.0</modelVersion> 
4.  <groupId>ActiveMQ-Spring</groupId> 
5.  <artifactId>ActiveMQ-Spring</artifactId> 
6.  <packaging>war</packaging> 
7.  <name /> 
8.  <version>0.0.1-SNAPSHOT</version> 
9.  <description /> 
10.  <build> 
11.    <sourceDirectory>${basedir}/src</sourceDirectory> 
12.    <outputDirectory>${basedir}/WebRoot/WEB-INF/classes</outputDirectory> 
13.    <resources> 
14.      <resource> 
15.        <directory>${basedir}/src</directory> 
16.        <excludes> 
17.          <exclude>**/*.java</exclude> 
18.        </excludes> 
19.      </resource> 
20.    </resources> 
21.    <plugins> 
22.      <plugin> 
23.        <artifactId>maven-war-plugin</artifactId> 
24.        <configuration> 
25.          <webappDirectory>${basedir}/WebRoot</webappDirectory> 
26.          <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory> 
27.        </configuration> 
28.      </plugin> 
29.      <plugin> 
30.        <artifactId>maven-compiler-plugin</artifactId> 
31.        <configuration> 
32.          <source>1.5</source> 
33.          <target>1.5</target> 
34.        </configuration> 
35.      </plugin> 
36.    </plugins> 
37.  </build> 
38.  <dependencies> 
39.    <dependency> 
40.      <groupId>org.apache.openejb</groupId> 
41.      <artifactId>javaee-api</artifactId> 
42.      <version>5.0-1</version> 
43.      <scope>provided</scope> 
44.    </dependency> 
45.    <dependency> 
46.      <groupId>javax.faces</groupId> 
47.      <artifactId>jsf-api</artifactId> 
48.      <version>1.2_04</version> 
49.      <scope>provided</scope> 
50.    </dependency> 
51.    <dependency> 
52.      <groupId>javax.servlet</groupId> 
53.      <artifactId>jstl</artifactId> 
54.      <version>1.2</version> 
55.      <scope>provided</scope> 
56.    </dependency> 
57.    <dependency> 
58.      <groupId>javax.servlet.jsp</groupId> 
59.      <artifactId>jsp-api</artifactId> 
60.      <version>2.1</version> 
61.      <scope>provided</scope> 
62.    </dependency> 
63.    <dependency> 
64.      <groupId>javax.faces</groupId> 
65.      <artifactId>jsf-impl</artifactId> 
66.      <version>1.2_04</version> 
67.      <scope>provided</scope> 
68.    </dependency> 
69.    <dependency> 
70.        <groupId>org.apache.activemq</groupId> 
71.        <artifactId>activemq-all</artifactId> 
72.        <version>5.2.0</version> 
73.    </dependency> 
74.    <dependency> 
75.        <groupId>org.springframework</groupId> 
76.        <artifactId>spring-core</artifactId> 
77.        <version>2.5.6</version> 
78.    </dependency> 
79.    <dependency> 
80.        <groupId>org.springframework</groupId> 
81.        <artifactId>spring-jms</artifactId> 
82.        <version>2.5.6</version> 
83.    </dependency> 
84.  </dependencies> 
85.</project> 






Java代码 
1.package com.jeff.mq; 
2. 
3.import javax.jms.Destination; 
4.import javax.jms.JMSException; 
5.import javax.jms.TextMessage; 
6. 
7.import org.springframework.context.ApplicationContext; 
8.import org.springframework.context.support.ClassPathXmlApplicationContext; 
9.import org.springframework.jms.core.JmsTemplate; 
10. 
11./**
12.* 消息接收者
13.*
14.* @author jeff
15.*/ 
16.public class MyReceiver { 
17.        public static void main(String[] args) throws JMSException { 
18.                ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); 
19.                JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate"); 
20.                Destination destination = (Destination) ctx.getBean("destination"); 
21.                while (true) { 
22.                        TextMessage txtmsg = (TextMessage) template.receive(destination); 
23.                        if (null != txtmsg) 
24.                                System.out.println("收到消息内容为: " + txtmsg.getText()); 
25.                        else 
26.                                break; 
27.                } 
28.        } 
29.} 






Java代码 
1.package com.jeff.mq; 
2. 
3.import javax.jms.Destination; 
4.import javax.jms.JMSException; 
5.import javax.jms.Message; 
6.import javax.jms.Session; 
7. 
8.import org.springframework.context.ApplicationContext; 
9.import org.springframework.context.support.ClassPathXmlApplicationContext; 
10.import org.springframework.jms.core.JmsTemplate; 
11.import org.springframework.jms.core.MessageCreator; 
12. 
13./**
14.* 消息发送者
15.*
16.* @author jeff
17.*/ 
18.public class MySender { 
19.        public static void main(String[] args) { 
20.                ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); 
21.                JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate"); 
22.                Destination destination = (Destination) ctx.getBean("destination"); 
23. 
24.                template.send(destination, new MessageCreator() { 
25.                        public Message createMessage(Session session) throws JMSException { 
26.                                return session.createTextMessage("发送消息:Hello ActiveMQ Text Message!"); 
27.                        } 
28.                }); 
29.                System.out.println("成功发送了一条JMS消息"); 
30.        } 
31.} 



Spring配置文件




Xml代码 
1.<?xml version="1.0" encoding="UTF-8"?> 
2.<beans 
3.    xmlns="http://www.springframework.org/schema/beans" 
4.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
5.    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
6. <!-- 配置JMS连接工厂 --> 
7.        <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory"> 
8.                <property name="brokerURL" value="tcp://localhost:61616"/> 
9.        </bean> 
10. 
11.        <!-- 配置JMS模版 --> 
12.        <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 
13.                <property name="connectionFactory" ref="connectionFactory"/> 
14.        </bean> 
15. 
16.        <!-- 发送消息的目的地(一个队列) --> 
17.        <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> 
18.                <!-- 设置消息队列的名字 --> 
19.                <constructor-arg index="0" value="HelloWorldQueue"/> 
20.        </bean>  
21. 
22. 
23. 
24.</beans> 
  
运行发送端三次:


成功发送了一条JMS消息

Process finished with exit code 0



然后再运行接收端一次:


收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!
收到消息内容为: 发送消息:Hello ActiveMQ Text Message!



继续测试发现,接收端接收一条消息后不退出程序,而是继续等待,一旦有消息发送过来,就获取到,然后输出!
1. 用户与权限管理模块 角色管理: 学生:查看实验室信息、预约设备、提交耗材申请、参与安全考核 教师:管理课题组预约、审批学生耗材申请、查看本课题组使用记录 管理员:设备全生命周期管理、审核预约、耗材采购与分发、安全检查 用户操作: 登录认证:统一身份认证(对接学号 / 工号系统,模拟实现),支持密码重置 信息管理:学生 / 教师维护个人信息(联系方式、所属院系),管理员管理所有用户 权限控制:不同角色仅可见对应功能(如学生不可删除设备信息) 2. 实验室与设备管理模块 实验室信息管理: 基础信息:实验室编号、名称、位置、容纳人数、开放时间、负责人 功能分类:按学科(计算机实验室 / 电子实验室 / 化学实验室)标记,关联可开展实验类型 状态展示:实时显示当前使用人数、设备运行状态(正常 / 故障) 设备管理: 设备档案:名称、型号、规格、购置日期、单价、生产厂家、存放位置、责任人 全生命周期管理: 入库登记:管理员录入新设备信息,生成唯一资产编号 维护记录:记录维修、校准、保养信息(时间、内容、执行人) 报废处理:登记报废原因、时间,更新设备状态为 "已报废" 设备查询:支持按名称、型号、状态多条件检索,显示设备当前可用情况 3. 预约与使用模块 预约管理: 预约规则:学生可预约未来 7 天内的设备 / 实验室,单次最长 4 小时(可设置) 预约流程:选择实验室→选择设备→选择时间段→提交申请(需填写实验目的) 审核机制:普通实验自动通过,高危实验(如化学实验)需教师审核 使用记录: 签到 / 签退:到达实验室后扫码签到,离开时签退,系统自动记录实际使用时长 使用登记:填写实验内容、设备运行情况(正常 / 异常),异常情况需详细描述 违规管理:迟到 15 分钟自动取消预约,多次违规限制预约权限 4. 耗材与安全管理模块 耗材管理: 耗材档案:名称、规格、数量、存放位置、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值