Java Message Service

watermelonroachdaleInternet and Web Development

Jul 30, 2012 (4 years and 8 months ago)

223 views

JMS AND MOM

Dave Elliman

Messaging

Messaging is a model in which applications are
loosely coupled

through the exchange of
self
-
describing

messages.

Message Oriented Middleware (MOM)
encompasses
publish/subscribe

and
message
queuing
communications.

Sender

Receiver

MOM

Messages

3

Messages vs. RPC


Why uses Messages?

Messages provides
loose coupling


Large scale systems: RPC often too
tightly coupled


Asynchronous interaction possible


Sender up while receiver down (or
disconnected) and vice
-
versa
-

robust


Messages are
highly flexible
and can
enable

load sharing

What is a message?


Anything can be a message : string,
object, XML, JSON


Arbitrary interaction patterns
possible


1
-
1, n
-
n, with replies, with no replies

Messaging Models

Topic

Subscriber

Subscriber

Publisher

Publish and Subscribe ( 1
-
> many )

(pub/sub)

Queue

Receiver

Receiver

Sender

Point
-
to
-
Point ( 1
-
> 1 )

(p2p, PTP)

6

Interaction Styles


Two main
mode of message consumption

on the receiver:


Blocking

(synchronous, pull mode) with


MessageConsumer.receive
()


Non
-
blocking

(asynchronous, push mode)
with:


MessageListener.onMessage(..)

7

Typically used with point
-
to
-
point queues

Producer

Queue

Consumer

send(m1)

send(m3)

send(m2)

receive()

m1

Put message into queue

Consume message

Active Reception (Pull)

8

Passive Reception (Push)

Typically used with 1
-
n communication (Publish/Subscribe)

Producer

Broker

Consumer

Consumer

Pass message to broker

publish(m1)

send(m2)

send(m2)

send(m1)

send(m1)

Dispatch message to all
consumers

publish(m2)

publish(m3)

9

What is JMS?


Java Message Service


An API that is part of the JEE standard
(since Java 1.3)


Relevant classes and interface in
javax.jms


Supported by all JEE application servers:
JBoss, Glassfish, BEA WebLogic, IBM
WebSphere

About JMS


JMS provides a Message Oriented
Middleware (MOM)


Clients communicate via “queues” and
“topics”


Queues and topics can provide different
degrees of guarantee, persistence,
atomicity, blocking/non blocking


Queues and topics are managed by a
central server


Server might be distributed / replicated for
performance / reliability

Widely used MOMs


JMS already described


RabbitMQ (written in Erlang)


MSMQ (Microsoft)


Amazon (SQS) Simple Queue Service


WebSphere MQ (IBM)


Cocoa’s NSOperationQueue

12

Typical JMS Architecture

JMS Server

Naming

Service

registers queues
and topics objects

Message
Producer

Message
Consummer

Lookup JMS
server, queues
and topics

Set up a
connection to
the server, and
session to a
queue or topic

Lookup ...

Set up ...

m

m

JMS API

Implementation dependent

Message Oriented Middleware

Guaranteed Delivery

Application A

Messaging API

Messaging Adapter

Application B

Messaging API

Messaging Adapter

Message
-

Oriented

Middleware

Store &

Forward

I had a Problem

(Coursemarker for C++)


I did not want to be tied into using
Windows on the server side (ok to
support Windows clients)


I did not want to be tied into Java
either (even using Groovy)


My server is Linux and my software
written in Python


Is there a MOM for me?

what do you need?


There are really two parts to my
problem
-

and this is the usual clase


A Producer task when a student
submits


A Consumer task that does the
assessment and feedback

Easy.. Use RabbitMQ
and Celery


“celery” handles the tasks



RabbitMQ
” handles the messaging


Celery


http://ask.github.com/celery


I can even use JSON for the
messages


Wonderfully easy to use:

from
celery.decorators

import task


@task(serializer=
"json"
)

def
mark_submission
(summission):


code here.....
.

The producer calls the task


This now carries on immediately
leaving my web application responsive


Why can this also help with scaling?

mark_submission.delay(submission)

And the message
Queue?


I tested it using a database as the
message queue which celery supports


This was done by inserting a single
line:


celery,


in my settings.py file for django


Not the best solution long term as
does not help with scaling

There is a really fast, effective,
scalable , reliable, vender
-
neutral MOM solution available
now

http://www.rabbitmq.com
/