JMS入门教程:Java消息服务的核心概念与应用

需积分: 31 23 下载量 2 浏览量 更新于2024-07-24 收藏 711KB PDF 举报
"JMS简明教程.pdf" 这篇教程介绍了Java消息服务(JMS,Java Message Service)的基础知识,包括其目标、功能、消息系统的基本概念以及JMS在企业级应用中的角色。JMS是一个接口规范,允许Java程序员以标准化的方式与企业消息系统交互,促进不同业务组件之间的可靠通信。 1. **JMS的目标与功能**:JMS的主要目标是提供一个通用接口,让Java应用程序能够访问各种企业消息产品。它简化了Java程序员使用消息系统的复杂性,增强了消息应用的可移植性。JMS提供了一组接口和语义,定义了客户端如何与消息系统进行创建、发送、接收和查看消息。 2. **消息系统概述**:消息系统是点对点通信工具,通常由消息代理管理,提供消息的创建、发送和接收功能。消息可以广播到多个目的地,也可以发送到单一目的地。消息系统还提供异步或同步接收消息的能力,以及保证消息传递的策略,如时效性、优先级和响应要求。 3. **JMS消息**:JMS定义了一系列消息接口,客户端使用提供商提供的消息实现进行交互。JMS的重要目标是确保客户端能使用统一的API来创建和处理消息,同时保持与提供商的独立性。 4. **JMS域**:JMS支持两种消息域,点对点(PTP)和发布/订阅(Pub/Sub)。PTP围绕消息队列进行,消息从一个客户端发送到特定队列,由其他客户端接收。Pub/Sub模式中,发布者将消息发布到主题,订阅者可以动态订阅并接收这些消息。 5. **JMS的可移植性**:JMS的设计目标是让应用能在同一消息域内跨产品使用,同时也期望JMS客户端能够在不同的硬件和操作系统上运行,前提是使用相同JMS提供商。 6. **JMS不包含的功能**:JMS不包含的功能包括事务、安全性、多线程、触发式客户端和请求/回复机制等,这些需要通过其他Java API如JDBC、JavaBeans、EJB、JTA、JTS、JNDI和J2EE平台来协同实现。 7. **JMS1.1新特性**:教程提到了JMS1.1版本的新特性,但具体细节未给出。 8. **JMS与Java其他API的关系**:JMS与JDBC、JavaBeans、EJB、JTA、JTS、JNDI和J2EE平台有紧密联系,它们共同构建了企业级应用的完整架构,JMS在其中负责消息传递。 9. **JMS应用架构**:JMS应用由JMS客户端组成,这些客户端定义消息并与其交互。JMS应用的管理和安全是开发过程中的重要环节。JMS支持两种消息风格,开发JMS应用需要理解这些风格并正确使用JMS接口。 10. **JMS消息模型**:教程还涵盖了JMS消息模型的背景、目标和消息结构,帮助读者理解消息在JMS框架中的核心作用。 通过这个教程,开发者能够掌握JMS的基本概念和使用方法,为构建基于JMS的企业级应用打下坚实基础。

详细说一下一下代码:package com.mcloud.market.mq; import com.mcloud.common.constant.Constants; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.config.JmsListenerContainerFactory; import org.springframework.jms.config.SimpleJmsListenerContainerFactory; import org.springframework.jms.core.JmsMessagingTemplate; import javax.jms.ConnectionFactory; import javax.jms.Queue; @Configuration public class ActiveMQConfig { @Value("${spring.activemq.broker-url}") private String brokerUrl; @Value("${spring.activemq.user}") private String username; @Value("${spring.activemq.password}") private String password; @Bean public Queue queue() { return new ActiveMQQueue(Constants.PREFIX + ".amount"); } @Bean(name = "messageQueue") public Queue amountQueue() { return new ActiveMQQueue(Constants.PREFIX + ".message"); } // 在Queue模式中,对消息的监听需要对containerFactory进行配置 @Bean("queueListener") public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory) { SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); factory.setPubSubDomain(false); return factory; } @Bean public ConnectionFactory connectionFactory() { return new ActiveMQConnectionFactory(username, password, brokerUrl); } @Bean public JmsMessagingTemplate jmsMessageTemplate() { return new JmsMessagingTemplate(connectionFactory()); } }

2023-06-06 上传

WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dtoMapper': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demandResponseEventMapper': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demandResponseEventService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demandResponseEventPublisher': Unsatisfied dependency expressed through field 'jmsTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'l' defined in class path resource [com/avob/openadr/server/common/vtn/VtnConfigRabbitmqBroker.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.jms.core.JmsTemplate]: Factory method 'externalJmsTemplate' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'externalConnectionFactory' defined in class path resource [com/avob/openadr/server/common/vtn/VtnConfigRabbitmqBroker.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.jms.ConnectionFactory]: Factory method 'externalConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: com/rabbitmq/jms/admin/RMQConnectionFactory

2023-06-10 上传