1 jms as xml and object-based messaging integration infrastructure michael wynholds founder carbon...
TRANSCRIPT
![Page 1: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/1.jpg)
1
JMS as XML and Object-Based Messaging Integration Infrastructure
Michael WynholdsFounderCarbon [email protected]
![Page 2: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/2.jpg)
2
THIS PRESENTATION
• Basics of JMS – 10%• Types of integration – 50%
– Integrate system components– Integrate dependent objects in EJBs
• Message payloads – 20%– Java objects– XML
• Q & A – 20%
![Page 3: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/3.jpg)
3
WHAT IS JMS?
• Java Messaging Service• Java API for enterprise messaging
– Non-Java implementations available• Multiple messaging paradigms
– Point-to-point– Publish/Subscribe
![Page 4: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/4.jpg)
4
REAL WORLD EXAMPLE #1
Major Components
vs.
Sub-Components
![Page 5: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/5.jpg)
5
INTEGRATING SYSTEM COMPONENTS
• Major components– Crucial serial functionality– Affects user experience– Example: shopping basket
• Sub-components– Possibly crucial functionality– Slight delays acceptable– Example: Email notification engine
![Page 6: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/6.jpg)
6
THE WRONG WAYProcesses happening serially:
– User does something in major component– Waits for sub-component to finish before
continuing on through the application
![Page 7: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/7.jpg)
7
THE WRONG WAYTwo things wrong:
– Why should the user wait?– What if we decide later we want to perform other
non-crucial actions at this time?
![Page 8: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/8.jpg)
8
THE RIGHT WAY:ASYNCHRONOUS
![Page 9: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/9.jpg)
9
ASYNCHRONOUS
• Fire and continue• Can still have guaranteed delivery• Sub-components can be moved to other
machines
![Page 10: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/10.jpg)
10
THE RIGHT WAY:LOOSE COUPLING
![Page 11: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/11.jpg)
11
LOOSE COUPLING• Determine significant events• Major components publish• Sub-components subscribe• Addition or modification of sub-component
requires no change to major component
![Page 12: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/12.jpg)
12
EXAMPLE:EMAIL NOTIFICATION
• Pet Store• What are significant events?
– User logs in– User status is changed– User purchases pet– Shipment is delayed (from another publisher)
![Page 13: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/13.jpg)
13
SIGNIFICANT EVENT XML<event type="user-status-change"> <context> <timestamp>2001-03-17 12:42:06 PST</timestamp> <user> <first-name>Guy</first-name> <last-name>Incognito</last-name> <email>[email protected]</email> <status>ADMINISTRATOR</status> </user> </context>
<user> <first-name>Michael</first-name> <last-name>Wynholds</last-name> <email>[email protected]</email> <status>REGULAR</status> </user>
<new-status>PREFERRED</new-status></event>
![Page 14: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/14.jpg)
14
SIGNIFICANT EVENT OBJECTS
![Page 15: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/15.jpg)
15
NOTIFICATION ENGINE CODEprivate void init() throws NamingException, JMSException{ Context ctx = JNDIUtil.getContext(); String selector = new StringBuffer() .append("type = ").append(MessageTypes.CARBONFIVE_EVENT) .toString();
tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY); tcon = tconFactory.createTopicConnection(); tsession = tcon.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); try { topic = (Topic) ctx.lookup(TOPIC_NAME); } catch (NamingException ne) { topic = tsession.createTopic(TOPIC_NAME); ctx.bind(TOPIC_NAME, topic); } tsubscriber = tsession.createSubscriber(topic, selector, false); tsubscriber.setMessageListener(this); tcon.start();}
![Page 16: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/16.jpg)
16
public void onMessage(javax.jms.Message msg){ synchronized (this) { int type = msg.getIntProperty("type"); Event event = (Event) ((ObjectMessage) msg).getObject(); switch (msg.getIntProperty("type")) { case MessageTypes.USER_STATUS_CHANGE: Email email = generateEmail((UserStatusChangeEvent) event); email.send(); break;
default: break; } }}
NOTIFICATION ENGINE CODE
![Page 17: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/17.jpg)
17
private Email generateEmail(UserStatusChangeEvent event){ Email email = new Email(); email.setTo(event.getUser().getEmail()); email.setFrom("[email protected]"); email.setSubject("Your status has changed"); email.setBody(getBody()); return email;}
NOTIFICATION ENGINE CODE
![Page 18: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/18.jpg)
18
MESSAGE SENDER CODE
private void init() throws NamingException, JMSException{ Context ctx = JNDIUtil.getContext();
tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY); tcon = tconFactory.createTopicConnection(); tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); try { topic = (Topic) ctx.lookup(TOPIC_NAME); } catch (NamingException ne) { topic = tsession.createTopic(TOPIC_NAME); ctx.bind(TOPIC_NAME, topic); } tpublisher = tsession.createPublisher(topic);}
![Page 19: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/19.jpg)
19
public void send(Serializable obj, int type) throws JMSException{ if (obj == null) { throw new NullPointerException("obj is null"); } ObjectMessage msg = tsession.createObjectMessage(); msg.setObject(obj); msg.setIntProperty("type", type); tpublisher.publish(msg); msg = null; // gc}
MESSAGE SENDER CODE
![Page 20: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/20.jpg)
20
USER STATUS CHANGE CODE
private void handleStatusChange(ServletRequest request){ HttpSession session = request.getSession(true);
User user = UserManager.getUserById(request.getParameter("user_id")); User me = session.getCurrentUser();
UserStatusChangeEvent event = new UserStatusChangeEvent(); EventContext ctx = new EventContext(); ctx.setUser(me); ctx.setTimestamp(new Date()); event.setContext(ctx); event.setUser(user); event.setNewStatus(request.getParameter("new_status")); MessageSender.send(event, MessageTypes.USER_STATUS_CHANGE_EVENT);
user.setStatus(request.getParameter("new_status"));}
![Page 21: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/21.jpg)
21
Entity EJB persistence optimization
REAL WORLD EXAMPLE #2
![Page 22: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/22.jpg)
22
ENTITY EJB PERSISTENCE• Entity EJB guarantees synchronization
with persistent store• Overhead in EJB container• Coarse-grained Entity Beans are better• isModified() method often used to optimize
• So what’s the problem?
![Page 23: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/23.jpg)
23
THE PROBLEM
• Multiple beans modify same dependent object• Bean instance may be on separate machines
![Page 24: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/24.jpg)
24
SOLUTION #1• Make dependent object an EJB• No longer coarse-grained
– EJB overhead takes toll
• Multiple machine problem not fixed
![Page 25: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/25.jpg)
25
• Don’t use isModified()• Slooooooowwwwwww….• Every getter hits the database
– Something may have changed– But most of the time, nothing has changed
SOLUTION #2
![Page 26: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/26.jpg)
26
• JMS-based isModified()• Dependent object publishes to topic when
a setter is called• Entity Beans subscribe to events
corresponding to their dependent objects• Efficient• Spans multiple machines
SOLUTION #3
![Page 27: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/27.jpg)
27
DEPENDENT OBJECT CODEpublic class Thing{ private String name;
public void setName(String name) { this.name = name; fireModificationEvent(this.getClass().getName(), this.hashCode()); }}
![Page 28: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/28.jpg)
28
EJB isModified()private void init() throws NamingException, JMSException{ Context ctx = JNDIUtil.getContext(); String selector = new StringBuffer() .append(“class = ”) .append(Thing.class.getName()) .toString();
tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY); tcon = tconFactory.createTopicConnection(); tsession = tcon.createTopicSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); try { topic = (Topic) ctx.lookup(TOPIC_NAME); } catch (NamingException ne) { topic = tsession.createTopic(TOPIC_NAME); ctx.bind(TOPIC_NAME, topic); } tsubscriber = tsession.createSubscriber(topic, selector, false); tsubscriber.setMessageListener(this); tcon.start();}
![Page 29: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/29.jpg)
29
EJB isModified()public void onMessage(javax.jms.Message msg){ synchronized (this) { ModEvent event = (ModEvent) ((ObjectMessage) msg).getObject(); if (Thing.class.getName().equals(event.getClassName())) { if (this.thing.hashCode == event.getHashCode()) { this.setModified(true); } } }}
![Page 30: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/30.jpg)
30
ISSUES• Class-based, not object-based events
– May receive many messages when using common objects
• hashCode() method not always unique– Just means you do a database hit
• Must implement hashCode() in objects to work across machines
• Can use hash code in selector– Which is more efficient?– I don’t know.
![Page 31: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/31.jpg)
31
MESSAGE PAYLOADS
• Objects– Rule of thumb: Use objects when you can
use objects.
• XML– Rule of thumb: Use XML when you need to
use XML.
![Page 32: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/32.jpg)
32
OBJECT MESSAGES• Easy to create / use• Little processing overhead• No need for XML tools• Better within same JVM or when JVM is
guaranteed• Full functionality of Java inside message
![Page 33: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/33.jpg)
33
XML MESSAGES• Can be much smaller size
– Better for high-traffic distributed systems
• Potential direct interaction with receiving systems
• Producer or consumer may be non-Java JMS client
• No class sync needed
![Page 34: 1 JMS as XML and Object-Based Messaging Integration Infrastructure Michael Wynholds Founder Carbon Five mike@carbonfive.com](https://reader036.vdocuments.us/reader036/viewer/2022062511/55147d7d550346b2598b48c7/html5/thumbnails/34.jpg)
34
REFERENCES
• http://www.carbonfive.com/oreilly
Contains links to this and other Carbon Five presentations.