第一步:SonicMQ的安装准备
1、安装SonicMq7.0,一切按默认配置(Domain1,tcp://localhost:2506,Administrator,Administrator),在程序菜单中生产“SonicMQ DomainManager”,“SonicMQ Management Console”等。
2、先启动“SonicMQ DomainManager”,然后在“SonicMQ Management Console”界面,选择Tools/JMS Administered Objects菜单中,打开一个面板,使用第二个选项“JNDI Naming Service”,并选中“Sonic Storage”,填写安装是输入的连接信息,连接。连接后分别在“Destinations”和“Connection Factories”面板创建一个Queue("SampleQ1")和Connection Factory("seashellSonicSendXAConnectionFactory")。
第二步:Spring JMS配置文件(jmsconfig.xml)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- jndiTemplate -->
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
com.sonicsw.jndi.mfcontext.MFContextFactory
</prop>
<prop key="java.naming.provider.url">tcp://localhost:2506</prop>
<prop key="com.sonicsw.jndi.mfcontext.domain">Domain1</prop>
<prop key="com.sonicsw.jndi.mfcontext.idleTimeout">60000</prop>
<prop key="java.naming.security.principal">Administrator</prop>
<prop key="java.naming.security.credentials">Administrator</prop>
</props>
</property>
</bean>
<!-- JMS队列 -->
<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="SampleQ1" />
<property name="jndiTemplate">
<ref local="jndiTemplate"></ref>
</property>
</bean>
<!-- JMS连接工厂 -->
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="seashellSonicSendXAConnectionFactory" />
<property name="jndiTemplate" ref="jndiTemplate" />
</bean>
<bean id="cachedConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="connectionFactory"/>
</bean>
<!-- jmsTemplate -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="cachedConnectionFactory" />
</property>
<property name="sessionTransacted" value="true" />
<property name="sessionAcknowledgeModeName" value="SESSION_TRANSACTED" />
</bean>
<!-- JMS 消息监听容器 -->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="cachedConnectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="messageListener" />
<property name="concurrentConsumers" value="2" />
<property name="sessionTransacted" value="true" />
</bean>
<!-- Message Driven POJO (MDP 消息驱动POJO) -->
<bean id="messageListener" class="com.tsinghuatec.dawn.waf.jms.JmsQueueListener" />
<!-- JMS 消息发送者 配置-->
<bean id="jmsQueueSender" class="com.tsinghuatec.dawn.waf.jms.JmsQueueSender">
<property name="jmsTemplate" ref="jmsTemplate"/>
<property name="queue" ref="destination" />
</bean>
</beans>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:jmsconfig.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
第三步:Java Source:
服务端MDB(com.tsinghuatec.dawn.waf.jms.JmsQueueListener)
public class JmsQueueListener implements MessageListener {
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
String msg = ((TextMessage) message).getText();
System.out.println("已经收到消息:" + msg);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
}
客户端(com.tsinghuatec.dawn.waf.jms.JmsQueueSender)
public class JmsQueueSender {
private JmsTemplate jmsTemplate;
private Queue queue;
public void send(final String message) {
jmsTemplate.send(queue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
public void setJmsTemplate(JmsTemplate template) {
this.jmsTemplate = template;
}
public void setQueue(Queue queue) {
this.queue = queue;
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:jmsconfig.xml");
JmsQueueSender sender = (JmsQueueSender) context.getBean("jmsQueueSender");
sender.send("test a message");
}
}
第四步:将服务端MDB打jar包放在WEB-INF/lib下,运行客户端JmsQueueSender。
输出结果:
已经收到消息:test a message
已经收到消息:test a message
已经收到消息:test a message
分享到:
相关推荐
2. **异步通信**:消息可以在任意时间发送和接收,这使得系统能够处理高负载情况下的峰值流量。 3. **事务支持**:JMS支持本地和全局事务,确保消息的可靠传输和处理。 4. **持久化**:即使消息消费者暂时不可用,...
消息队列允许应用程序之间进行异步通信,从而提高系统的性能和可靠性。 消息队列的特点包括解耦、异步、可靠、灵活和可伸缩。解耦意味着发送者不需要知道接收者的任何信息,异步意味着发送者发送完消息后可以立即...
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。
把代码放到Word中,通过开发工具——Visual Basic——插入模块,粘贴在里在,把在硅基流动中申请的API放到VBA代码中。在Word中,选择一个问题,运行这个DeepSeekV3的宏就可以实现在线问答