ActiveMQ之二:JMS介绍与ActiveMQ介绍

此文转载 ............

 

1JMS介绍

JMS源于企业应用对于消息中间件的需求,使应用程序可以通过消息进行异步处理而互不影响。Sun公司和它的合作伙伴设计的JMS API定义了一组公共的应用程序接口和相应语法,使得Java程序能够和其他消息组件进行通信。JMS有四个组成部分:JMS服务提供者、消息管理对象、消息的生产者消费者和消息本身。

 1)、JMS服务提供者实现消息队列和通知,同时实现消息管理的APIJMS已经是J2EE       API的一部分,J2EE服务器都提供JMS服务。

 

2)、消息管理对象提供对消息进行操作的APIJMS API中有两个消息管理对象:创建jms连接使用的工厂(ConnectionFactory)和目的地(Destination),根据消息的消费方式的不同ConnectionFactory可以分为QueueConnectionFactoryTopicConnectionFactory,目的地(Destination)可以分为队列(Queue)和主题(Topic)两种。

3)、消息的生产者和消费者。消息的产生由JMS的客户端完成,JMS服务提供者负责管理这些消息,消息的消费者可以接收消息。消息的生产者可以分为――点对点消息发布者(P2P)和主题消息发布者(TopicPublisher)。所以,消息的消费者分为两类:主题消息的订阅者(TopicSubscriber)和点对点消息的接收者(queue receiver

4)、消息。消息是服务提供者和客户端之间传递信息所使用的信息单元。JMS消息由以下三部分组成: 

  消息头(header――JMS消息头包含了许多字段,它们是消息发送后由JMS提供者或消息发送者产生,用来表示消息、设置优先权和失效时间等等,并且为消息确定路由。 

  属性(property――用来添加删除消息头以外的附加信息。 

  消息体(body――JMS中定义了5种消息体:ByteMessageMapMessageObjectMessageStreamMessageTextMessage 

 

2Messages 通信方式 

上面提到JMS通信方式分为点对点通信和发布/订阅方式 

1)、点对点方式(point-to-point 

   点对点的消息发送方式主要建立在 Message Queue,Sender,reciever上,Message Queue 存贮消息,Sneder 发送消息,receive接收消息.具体点就是Sender Client发送Message Queue , receiver ClierntQueue中接收消息和"发送消息已接受"Quere,确认消息接收。消息发送客户端与接收客户端没有时间上的依赖,发送客户端可以在任何时刻发送信息到Queue,而不需要知道接收客户端是不是在运行

2)、发布/订阅 方式(publish/subscriber Messaging 

    发布/订阅方式用于多接收客户端的方式.作为发布订阅的方式,可能存在多个接收客户端,并且接收端客户端与发送客户端存在时间上的依赖。一个接收端只能接收他创建以后发送客户端发送的信息。作为subscriber ,在接收消息时有两种方法,destinationreceive方法,和实现message listener 接口的onMessage 方法。

 

3、为什么选用ActiveMQ 

   1ActiveMQ是一个开放源码 

   2)基于Apache 2.0 licenced 发布并实现了JMS 1.1 

   3ActiveMQ现在已经和作为很多项目的异步消息通信核心了 

   4)在很多中小型项目中采用ActiveMQ+SPRING+TOMCAT开发模式。

 

4、编程模式 

4.1消息产生者向JMS发送消息的步骤 

 (1)创建连接使用的工厂类JMS ConnectionFactory

 (2)使用管理对象JMS ConnectionFactory建立连接Connection 

 (3)使用连接Connection 建立会话Session 

 (4)使用会话Session和管理对象Destination创建消息生产者MessageSender 

 (5)使用消息生产者MessageSender发送消息

4.2消息消费者从JMS接受消息的步骤 

 (1)创建连接使用的工厂类JMS ConnectionFactory 

 (2)使用管理对象JMS ConnectionFactory建立连接Connection 

 (3)使用连接Connection 建立会话Session 

 (4)使用会话Session和管理对象Destination创建消息消费者MessageReceiver 

 (5)使用消息消费者MessageReceiver接受消息,需要用setMessageListenerMessageListener接口绑定到MessageReceiver 

 消息消费者必须实现了MessageListener接口,需要定义onMessage事件方法。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Java通讯开发jms源代码 (jms通讯开发源码) java,net,socket,通讯开发,jms /* * @(#)Message.java 1.60 02/04/09 * * Copyright 1997-2002 Sun Microsystems, Inc. All Rights Reserved. * * SUN PROPRIETARY/CONFIDENTIAL. * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. * */ import java.util.Enumeration; public interface Message { String getJMSMessageID() throws JMSException; void setJMSMessageID(String id) throws JMSException; long getJMSTimestamp() throws JMSException; void setJMSTimestamp(long timestamp) throws JMSException; byte [] getJMSCorrelationIDAsBytes() throws JMSException; void setJMSCorrelationIDAsBytes(byte[] correlationID) throws JMSException; String getJMSCorrelationID() throws JMSException; Destination getJMSReplyTo() throws JMSException; void setJMSReplyTo(Destination replyTo) throws JMSException; Destination getJMSDestination() throws JMSException; void setJMSDestination(Destination destination) throws JMSException; int getJMSDeliveryMode() throws JMSException; void setJMSDeliveryMode(int deliveryMode) throws JMSException; boolean getJMSRedelivered() throws JMSException; void setJMSRedelivered(boolean redelivered) throws JMSException; String getJMSType() throws JMSException; void setJMSType(String type) throws JMSException; long getJMSExpiration() throws JMSException; void setJMSExpiration(long expiration) throws JMSException; int getJMSPriority() throws JMSException; void setJMSPriority(int priority) throws JMSException; void clearProperties() throws JMSException; boolean propertyExists(String name) throws JMSException; boolean getBooleanProperty(String name) throws JMSException; byte getByteProperty(String name) throws JMSException; short getShortProperty(String name) throws JMSException; int getIntProperty(String name) throws JMSException; long getLongProperty(String name) throws JMSException; float getFloatProperty(String name) throws JMSException; double getDoubleProperty(String name) throws JMSException; String getStringProperty(String name) throws JMSException; Object getObjectProperty(String name) throws JMSException; Enumeration getPropertyNames() throws JMSException; void setBooleanProperty(String name, boolean value) throws JMSException; void setByteProperty(String name, byte value) throws JMSException; void setShortProperty(String name, short value) throws JMSException; void setIntProperty(String name, int value) throws JMSException; void setLongProperty(String name, long value) throws JMSException; void setFloatProperty(String name, float value) throws JMSException; void setDoubleProperty(String name, double value) throws JMSException; void setStringProperty(String name, String value) throws JMSException; void setObjectProperty(String name, Object value) throws JMSException; void acknowledge() throws JMSException; void clearBody() throws JMSException; } 通讯开发必备源码资料!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值