Jms deep dive [con4864]
-
Upload
ryan-cuprak -
Category
Software
-
view
225 -
download
0
Transcript of Jms deep dive [con4864]
JMS DEEP DIVERyan Cuprak
About Me
Ryan Cuprak• @ctjava• [email protected]• http://www.cuprak.info• https://www.linkedin.com/in/rcuprak
Updated For• EE 7• EJB 3.2
Agenda
Custom Features
Best Practices
Gotchas
JMS Background
• Specification Revisions• JMS 1.0.2b – June 26, 2001• JMS 1.1 – April 12, 2002• JMS 2.0 – May 21, 2013• JMS 2.1 – Vote to Continue!
• Mandatory in Java EE 1.4 (November 11, 2003)!• MOM pre-dates JMS & Java EE
JMS Ecosystem
#1 Upgrade Code to EE 7
Major changes in Java EE 7 (JMS 2.0)Introduced in Java EE 7:• JMSContext – replaces Connection and Session• JMSProducer – replaces MessageProducer• JMSConsumer – replaces MessageConsumer
Best Practice
#1 Upgrade Code to EE 7…
Best Practice
#1 Upgrade Code to EE 7…
Best Practice
#2 ActiveMQ & Java EE 7
Gotcha
#2 Java EE 7: Stumper…
What???
Gotcha
#2 Java EE 7: Stumper…
http://tinyurl.com/h7cvl8bActiveMQ supports JMS 1.1 – not JMS 2!
Gotcha
#3 Exception Handling
What is the impact of a RuntimeException?
Bad!
Best Practice
#3 Exception Handling…
Runtime Exception:• Rollback and re-delivery of message• Destruction of MDB and possibly connection
Best Practice
#4 Which of these are optional?
1. JMSMessageID2. Overriding Message Header Fields 3. JMS Defined Properties4. Provider Specific Properties5. Distributed Transactions6. Multiple Sessions
All of them!
Best Practice
#5 Pre-fetching
• Message consumer (RAR) pre-fetches a set of messages to distribute to consumers (MDBs).
• Poorly configured pre-fetch• Starve other consumers• Heap exhaustion
• Default ActiveMQ: 1000 messages!
Gotcha
#4 Pre-fetching
Consumer starved!
Gotcha
#5 Large Messages
What happens to a container when you send gigantic messages with large pre-fetch?
Best Practice
#6 XA Transactions
How do you ensure that a messages is committed only if the database commits?
Best Practice
#6 XA Transactions
XA Data Source
Best Practice
#7 Wire Protocols
What are the different wire protocols and what are their unique features?• Openwire
• Cross language wire protocol for messaging• Native protocol for ActiveMQ
• AMQP – Advanced Message Queuing Protocol• ISO 19464 – binary wire protocol with widespread adoption• Designed for high performance interoperable messaging
• MQTT – Message Queue Telemetry Transport• Simple binary protocol – good for embedded/mobile
• Stomp – Simple/Streaming Text Oriented Messaging Protocol• Text based protocol with simple semantics
Are you using the right wire protocol?Best Practice
#7 Wire Protocols & MOMMOM OpenWire AMQP MQTT Stomp Other
OpenMQ X X
ActiveMQ X X X X
RabbitMQ X X X
HornetQ X XWebLogic X
WebSphere MQ
X X
• MQTT – IoT/embedded/mobile• Stomp – WebSockets – not high volume
Best Practice
#8 Dead Letter Queue
What is the dead letter queue and why should I care?• Message that is repeatedly refused by a client.• JMS provider will retry multiple times.• Messages ultimately moved to Dead Letter Queue
(DLQ)Order Queue Update Queue Update Queue
Update QueueBest Practice
#8 Dead Letter Queue…
• Hundreds of messages• How to recover?
Best Practice
#8 Dead Letter Queue…
ActiveMQ DLQ configuration – DLQ per queue.
Best Practice
#9 Expiring Messages
What’s wrong with the following code?
1. Message.setJMXExpiration() is for use by the container.
2. Ignored if you set it.
Gotcha
#9 Expired Messages & DLQ
• Expired messages dumped to DLQ• Messages build-up over time!
Best Practice
#9 Expired Messages & DLQ…
ActiveMQ solution
HornetQ
HornetQ messages are lost unless configured.Best Practice
#10 MDB Pools
Do I need to configure MDB pools?
Best Practice
#10 MDP Pools…
• Don’t accept default MDB pool sizes.• Monitor and adjust MDB pool sizes based on load.
glassfish-ejb-jar.xml
Best Practice
#11 Types of JMS Messages
What are the different types of messages types?StreamMessage Serialized stream of objects.
MapMessage Message composed of name/value pairs. Names must be unique.
TextMessage Simple message for Strings.
ObjectMessage Message consisting of a serialized Java Object.
BytesMessage Raw stream of bytes.
WebLogic includes XMLMessage:• Optimized for XML.• Supports filtering on XML content.
Best Practice
#12 Troubleshooting
What’s this message?
?Best Practice
#12 Troubleshooting
Set descriptive headers on the messages!
Best Practice
#13 Web Socket and JMS
Java EE 7 ServerWeb Socket
EndpointJMS Provider
JMS ProviderStomp over Web Sockets
Best Practice
#13 Web Socket and JMS…
• Stomp protocol is a simple text-oriented messaging protocol.
• Messaging server must support WebSockets• Initial handshake is HTTP
• Message providers supporting STOMP & WebSockets:• ActiveMQ• RabitMQ• WebLogic
• Enable for ActiveMQ:<transportConnector name="ws" uri="ws://0.0.0.0:61614"/>
Best Practice
#14 Delayed Message Delivery
Can I send a message but have it delayed?
Extended feature in ActiveMQ.• Send delay (AMQ_SCHEDULED_DELAY)• Re-send delay (AMQ_SCHEDULED_PERIOD)• Repeat count (AMQ_SCHEDULED_REPEAT)
Best Practice
#15 Priority & Messages
What does message priority mean?• Message priority scale: 0-9 (0: lowest 9: highest)• Default message priority: 4• JMS specification does not require a provider strictly implement message ordering.• Not dependable!
Best Practice
#15 Priority & Messages
Enforcing priority:• Apache Camel with Resequencer
• http://camel.apache.org/resequencer.html
• Use Selectors:• JMSPriority > 6• JMSPriority < 6
Best Practice
#16 Protocol Framing
So I need a bigger message frame?
104 MB
Best Practice
#16 Protocol Framing
Client exception:
Server ExceptionWARN | Transport Connection to: tcp://127.0.0.1:54025 failed: java.io.IOException: Frame size of 404 bytes larger
Best Practice
#17 Leaks & Variable Loads
Load testing always at 100%?Scenario:• Message Driven Bean with a connection to a remote
resource.• Standing pool size: 3
Gotcha
#17 Leaks & Variable Loads…
Oops, resource only leak as bean instances fluctuate!
1 2 3 4 5 6 70
5
10
15
20
25
30
35
LeakBean
Gotcha
#18 Advisory Messages
Extended ActiveMQ feature:• Consumers, producers and connections starting and
stopping• Temporary destinations being created and destroyed• messages expiring on topics and queues• Brokers sending messages to destinations with no
consumers.• Connections starting and stopping
Best Practice
#18 Advisory Messages…
Best Practice
#20 Know your Activation Spec Properties
Property Required Default DescriptionacknowledgeMode Auto-acknowledge Auto-acknowledge or Dups-ok-
acknowledge
clientId Set n RA Required for durable
destinationType X null Queue or Topic
Destination X null Destination name
enableBatch false Transaction batching
maxMessagesPerBatch 10 Message per transaction batch
maxMessagesPerSessions 10 Pre-fetch size
maxSessions 10 Max concurrent sessions
messageSelector null Selector
noLocal false Include local msg?
ActiveMQ
Best Practice
#20 Know your Activation Spec Properties
Property Default Descriptionpassword Set inRA Password for JMS connection
subscriptionDurability NonDurable NonDurable or Durable
subscriptionName null Name of subscription
userName Set in RA Username for JMS connection
useRAManagedTransaction false
initialRedeliveryDelay 1000 Re-delivery delay
maximumRedeliveries 5 Max re-deliveries
redeliveryBackOffMultiplier 5 Multiplier for back-off.
redeliveryUseExponentialBackOff false Configures exponential back-off.
ActiveMQ
Best Practice
#20 Know your Activation Spec Properties
• ActiveMQ – only 2 properties were required• Key tuning questions:
• Individual messages take a long time to process?• Many messages or few messages?• Batch processing of messages?• Ok to process duplicates?
Best Practice
#21 Configuration Conflicts
Default for configuration for an MDB and ActiveMQ RA:• Max MDB Pool Size (default 25)• Max Sessions (default 10)• MaxMessagesPerSessions (default 10)
How many messages can we process concurrently?
10Best Practice
#22 Configure Message Persistence
• JMS message providers message persistence is often configurable and tunable
• Concerns:• Available file/descriptors and disk space• I/O throughput • Flush frequencies
• ActiveMQ• KahaDB – file-based transaction data store (journaled) • AMQ message store – file based store, file per destination, index
must be rebuilt for non-planned termination• JDBC message store
Best Practice
#22 Configure Message Persistence…
API does exist for ActiveMQ KahaDB!Best Practice
#23 Deadlocked Consumer
Consumer blocks!• Pre-fetched messages are stuck in limbo• App cannot be un-deployed• App container may not shutdown properlyGotcha
#24 Multiple Destinations
• ActiveMQ supports sending messages to multiple queues and topics.
• Targets should be a comma separated list.• Use prefix topic:// or queue://
Extended Feature
#25 Multiple Destination Consumption• ActiveMQ supports consuming messages from multiple destinations – both
queues and topics• Destination naming:
• ‘.’ separates elements in a destination name• ‘*’ matches one element• ‘>’ matches one or all trailing elements
• Example topics:• us.ca.sfo.takeoffs• us.ca.sfo.landings• us.ca.lax.takeoffs• us.ct.bdl.takeoffs
• Patterns:• *.*.*.takeoffs – receive all takeoff messages• us.ca.> - all landings and takeoffs in CA
Extended Feature
#25 Multiple Destination Consumption…
Extended Feature
#26 GlassFish 4 Bug
Containers can have bugs!• GlassFish 3.x no message delay with MDBs and
ActiveMQ.• GlassFish 4.1.1 2 minute delay with messages from
ActiveMQ but NOT OpenMQ
Gotcha
#26 GlassFish 4 Bug…
Timeout increased to 2 minutes!
Gotcha
#26 GlassFish 4 Bug…
Code path was different for OpenMQ.
Gotcha
#27 Transactions
SaveException thrown
Single TransactionBest Practice
#27 Transactions…
To ensure item is saved:
Best Practice
#28 Concurrency
Following objects support concurrent access:• ConnectionFactory• Connection• DestinationFollowing object DO NOT support concurrent access:• Session• MessageProducer• MessageConsumer
Best Practice
#29 JMSX PropertiesProperty Matched Modified Description
JMSXUserID X X Id of user sending messageJMSXAppID X X App sending messageJMSXProducerTXID Producer transaction idJMSXConsumerTXID Transaction ID consumerJMSXRcvTimestamp Time delivered to consumerJMSXDeliveryCount X Delivery attemptsJMSXState Provider specific stateJMSXGroupID X X Message group idJMSXGroupSeq X X Sequence number in group
No all properties are supported by all providers!
Best Practice
#30 Message Groups
• Message Groups:• Guaranteed ordering of the processing of related messages across a
single queue• Load balancing across multiple consumers.• High availability / auto-failover
• Usage:• message.setStringProperty("JMSXGroupID", ”JavaOne");• message.setIntProperty("JMSXGroupSeq”,1);• message.setIntProperty("JMSXGroupSeq”,-1); // closes group
• ActiveMQ enhancement:• JMSXGroupFirstForConsumer
Best Practice
#31 Message Selectors
• Message selectors used to filter messages.• Selectors use subset of SQL92• Selectors cannot reference payload, only header
propertiesLiterals TRUE/FALSE, numbers, scientific notation
Identifiers Header or property field.
Operators AND, OR, LIKE, BETWEEN, =, <>, <, >, <=, =>, +, -, *, /, IS NULL, IS NOT NULL
Best Practice
Best Practices
• High availability (HA) for production• Protect queues/topics with passwords• Enable and configure SSL• Configure provider data store • Backup provider data store• Configure DQL policy• Monitor DLQ• Add descriptive message headers• Pick and tune messaging transport• Use Apache Camel and integration patterns
Join Us!
https://javaee-guardians.io
JMS 2.1
Vote for JMS 2.1!• Flexible MDBs (EE)• CDI beans as Listeners• Batch Delivery• Acknowledgment Modes• setMessageListener (EE)• create Connection factory (SE)• create Queue/Topic (SE)• Repeatable Annotations• Redelivery configuration (EE)
Q&A