• 1. JMS与消息服务分布式应用的一种方案与思考 Author : Yongxin Zhang
  • 2. 总揽进入Message Service JMS概述 JMS核心概念 消息模型-P2P 消息模型-Pub/Sub Example 高级主题 项目中的思考 参考资料 Q&A
  • 3. 进入 Message Service
  • 4. 分布式系统面临的问题、挑战异构系统集成(Heterogeneous Integration) 方案:FTP,Using the Database, Remote Procedure Call(RPC,HTTP,RMI-IIOP), Messaging, Web Services 系统瓶颈(System Bottlenecks) 方案:Cluster,Load Balance,RAC 可扩展性 灵活性 敏捷性 Agility is the ability to respond quickly to a constantly changing environment.
  • 5. 分布式系统目标 随着企业系统越来越复杂(complexity)和高精尖(sophistication),我们对系统提出了以下目标: Better reliability Increased scalability More flexibility
  • 6. 分布式模型-RPC (Tightly Coupled)
  • 7. 分布式模型-Enterprise Messaging
  • 8. Enterprise MessagingMessage和Messaging的关系 Message是一个载体,Messaging是使用Message这一载体的通道. A message is a self-contained package of business data and new routing headers. The messaging by which messages are transmitted from one application to another across a network.
  • 9. Messaging products也被称为MOM,Message-Oriented Middleware,消息中间件 商业产品:IBM WebSphere MQ , SonicMQ , Microsoft Message Queuing(MSMQ) and TIBCO Rendezvous , etc OpenSoure:Apache Active MQ(本文示例使用), etc
  • 10. Enterprise Messaging相关概念Web Services SOA(Services-Oriented Architecture) ESB(Enterprise Service Bus)
  • 11. Enterprise Messaging ArchitectursCentralized Architectures(hub and spoke)
  • 12. Enterprise Messaging ArchitectursDecentralized Architectures(IP multicast)
  • 13. Messaging Models消息服务的两种常用模型 Point-to-Point(P2P),点对点模型 类似生活中的打电话 Publisher-and-Subscriber(Pub/Sub),发布/订阅模型 类似生活中的看电视,听广播
  • 14. Messaging的优点Asynchronous operation A client can schedule work to be done and return Immediately A client can be notified in an event-driven fashion when something has happened Loose Coupling Systems connected via messaging need not be aware of each other, or use the same technologies Key messages can easily be routed to many systems Message broker is buffer in case of downtime
  • 15. Messaging的优点Fire and Forget Message Broker can guarantee messages are recorded and delivered even vs. crashes Parallelize Work Messages in a single stream can be handled in parallel by many client threads or systems Throttle Work A large amount of work can be split across a small number of threads or systems to throttle effort (e.g. due to licensing restrictions)
  • 16. JMS概述
  • 17. What is JMSThe Java API for messaging Included in Java EE, available standalone Key concepts include the message broker,message producers, message consumers, JMS topics vs. queues, and various message formats and processing options Includes a plain Java API (a little complex); simplified in Java EE, Spring, etc. − Those platforms simplify the process of establishing a connection to the message broker
  • 18. What is JMSJMS本身不是消息系统,它只是消息客户端连接消息系统所使用的接口或类的抽象,类似JNDI提供对访问不同数据的抽象。通过JMS,客户端可以非常方便访问各种提供了JMS实现的消息系统。 最初,JMS主要提供访问企业消息系统的Java API,到JMS 1.2,已发展成为一流的Java 分布式计算模型,就像基于RPC的CORBA,EJB.
  • 19. 几个需要区别的概念JMS Provider(Messaging System) JMS Client/Server Message producer&Consumer JMS Application
  • 20. JMS在Java EE中
  • 21. JMS Runtime
  • 22. JMS APIJMS API可分成三类主要的API 通用API Point-to-Point API Publish-and-Subscrip API
  • 23. JMS API 一览General APIP2P APIPub/Sub APIConnectionFactoryQueueConnectionFactoryTopicConnectionFactoryDestinationQueueTopicConnectionQueueConnectionTopicConnectionSessionQueueSessionTopicSessionMessageMessageMessageMessageProducerQueueSenderTopicPubliherMessageConsumerQueueReceiverTopicSubscriber
  • 24. JMS General API core interface
  • 25. JMS核心组成
  • 26. JMS components
  • 27. ConnectionFactory所有JMS核心组件中,ConnectionFactory和Destination必须通过JNDI从JMS Provider获取。 Is the base type for the TopicConnectionFactory and the QueueConnectionFactory Be used to create a Connection object.
  • 28. Destinationvirtual channels(called destinations) In all modern enterprise messaging systems, applications exchange messages through virtual channels. When sending a message, the message is addressed to a destination, not a specific application. Any application that subscribes or registers an interest in that destination may receive that message. Applications are decoupled. Is the base interface for the Topic and Queue
  • 29. ConnectionIs the base interface for the TopicConnection and the QueueConnection , and represents an open TCP/IP socket to the JMS provider.
  • 30. SessionA single-threaded context for producing and consuming messages Creates message consumers, producers, and messages for a specific JMS provider Manages the scope of transactions across send and receive operations, tracks message acknowledgment for consumers, and serializes delivery of messages to MessageListener objects: Is the base interface for the TopicSession and the QueueSession
  • 31. Producers&ConsumnerMessageProducer sends messages to a specified destination (Topic or Queue) Is the base interface for the TopicPublisher and the QueueSender.
  • 32. Producers&ConsumnerMessageConsumer can consume messages asynchronously or synchronously Is the base interface for the TopicSubscriber and the QueueReceiver.
  • 33. MessageThe most important part of the entire JMS Spec. All data and events in a JMS application are communicated with messages, while the rest of JMS exists to facilitate the transfer of messages Messages are the lifeblood of the system Has three parts: the message header, message properties, and finally the message data itself, called the payload or message body
  • 34. Message-HeadersProvide metadata about the message describing who or what created the message, when it was created, how long the data is valid Contain routing information Acknowledgement A lot more… Divided into two large groups: automatically assigned headers and developer-assigned headers
  • 35. Automatically Assigned HeadersJMSDestination JMSDeliveryMode JMSMessageID JMSTimestamp JMSExpiration JMSRedelivered JMSPriority
  • 36. Developer-assigned HeadersJMSReplyTo JMSCorrelationID JMSType
  • 37. Message-PropertiesAct like additional headers Three basic categories of message properties: application-specific properties,JMS-defined properties, and provider-specific properties
  • 38. Message结构-Message Type
  • 39. Synchronous vs. AsynchronousProducers are effectively synchronous Consumers may be synchronous or asynchronous: − Sync: poll the broker for a new message, maybe with a timeout. Control returns when message available. − Async: consumer provides a listener, and the listener is invoked when a new message is available
  • 40. AsynchronousA fundamental concept of messaging is that communication between applications is intended to be asynchronous Is the major difference between RPC and asynchronous messaging Is critical to understanding the advantages offered by messaging systems.
  • 41. MessageListenerReceives messages asynchronously from one or more Consumers (TopicSubscriber or QueueReceiver).
  • 42. Delivery ModeTwo types of delivery modes in JMS: persistent and nonpersistent Are used when establishing the delivery mode of a MessageProducer, TopicPublisher, or QueueSender A persistent message should be delivered once-and-only-once A nonpersistent message is delivered at-most-once
  • 43. Other Key ConceptsMessage Selectors Acknowledgement
  • 44. 消息模型-P2P
  • 45. Messaging Models-P2PPoint-to-Point(1-1)
  • 46. P2P OutlineThe producer is called a sender and the consumer is called a receiver Messages are exchanged through a virtual channel called a queue Each message is delivered to only one receiver Messages are ordered There is no coupling of the producers to the consumers
  • 47. When to Use P2P MessagingIs used when you want one receiver to process any given message once and only once Is used when you need synchronous communication between components, but those components are ritten in different programming languages or implemented in different technology platforms (e.g., J2EE and .NET) is used when to provide a higher degree of throughput to server-side components through the use of message-based load balancing
  • 48. JMS P2P API core interface
  • 49. ExampleLoan Request
  • 50. 消息模型-Pub/Sub
  • 51. Messaging Models-Pub/SubPublisher-and-Subscriber(1-many)
  • 52. Pub/Sub OutlineThe message producer is called a publisher and the message consumer a subscriber Messages are exchanged through a virtual channel called a topic Each message is delivered to multiple message consumers The publisher generally does not know and is not aware of which subscribers are receiving the topic messages Messages are pushed to consumers
  • 53. Pub/Sub OutlineAs in enterprise messaging in general, there is no coupling of the producers to the consumers Every client that subscribes to a topic receives its own copy of messages published to that topic Message selectors are applied when the message is copied to each subscriber; whereas with the p2p model, message selectors are applied after the message has been added to the queue Subscribers can be either durable or nondurable Subscribers may also be dynamic or administered
  • 54. When to Use Pub/Sub Messaging Is used when you want to broadcast a message or event to multiple message consumers
  • 55. JMS Pub/Sub API core interface
  • 56. Examplemortgage lender publishes mortgage rates and a borrower interested in the latest rates subscribes to the topic
  • 57. 高级主题
  • 58. Advanced TopicsMessage Filtering Guaranteed Messaging(reliability) Transacted Messaging(recovery) 消息使用者 -Java EE, Message-Driven Beans(MDB) -Spring,Message-Driven POJOs(MDP)
  • 59. Message FilteringTopic subscribers and queue receivers select what they want or interest. Filtering out certain messages from a queue and/or topic is done through message selectors. Message selectors are based on a subset of the SQL-92 conditional expression syntax Message selectors are made up of three elements: identifiers,literals, and comparison operators
  • 60. Guaranteed MessagingGuaranteed messaging is the key to understanding the design of a distributed messaging system. There are three main parts to guaranteed messaging message autonomy\ store-andforward the underlying message acknowledgment semantics
  • 61. Transacted MessagingTransacted producers Transacted consumers Transacted producers and consumers Distributed Transactions - A two-phase commit (2PC) - eXtended Architecture (XA) interface - JTA
  • 62. Java EE and MDB
  • 63. Spring and JMS
  • 64. 项目中的思考
  • 65. 消息思想的应用通知管理模块
  • 66. 当前系统的构造思考
  • 67. 参考资料
  • 68. 参考资料JMS Specification 1.2 Java Message Service , Second Edition JavaEE 1.6 Spec
  • 69. 深入思考-寻源万变不离其宗 面向对象思想 基于抽象设计、编程 多线程 分布式应用 TCP/IP 安全
  • 70. Q&A