Simple Java Messaging
Following up on my recent post Java Distributed Lock Manager, sometimes you just need a simple way to pass messages between Java processes.
Messaging is a very useful pattern in Enterprise Integration, and there are many ways to do it. Apache Camel is a great tool when you need the flexibility and power to manage complex messaging patterns, including routing, filtering and the like.
If you just want to do something simple, though, that can be a challenge. The most common solution, JMS, requires quite a bit of boilerplate code, and requires selecting and running a JMS provider, which means selecting a J2EE container, Apache ActiveMQ, or others.
So what if you just want a drop-dead simple way of adding messaging to your application? Terracotta gives you that. (And also integrates well with other solutions, like Apache Camel if you need more power later on).
Simple messaging in Terracotta is built on the notion of clustering a LinkedBlockingQueue. Just as a LinkedBlockingQueue is used to pass messages between threads in a single JVM, it will be used in combination with Terracotta's JVM-level clustering to provide message passing between JVMs.
To demonstrate, here is a simple example.
import java.io.*;The app consist of two modes - a receiver mode and a sender mode. Normally, you would have an application specific mechanism of choosing whether you wanted to send messages or receive messages. For this example, we use a simple lock (for more information on using a ReentrantReadWriteLock with Terracotta, read the ReentrantReadWriteLock recipe). When free, the lock indicates no processes are receiving messages, so the process takes on the "receiver" mode. All subsequent processes take on the "sender" mode when the lock is held.
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
public class SimpleMessage
{
private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private static BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
public static void receive() throws InterruptedException
{
System.out.println("Receiving messages...");
while (true) {
String msg = queue.take();
System.out.println("msg >> " + msg);
}
}
public static void send() throws Exception
{
while (true) {
System.out.print("Enter a message> "); System.out.flush();
String msg = new BufferedReader(new InputStreamReader(System.in)).readLine();
queue.put(msg);
}
}
public static void main(String[] args) throws Exception
{
// we use the presence of a lock to distinguish receiver from sender
if (lock.writeLock().tryLock()) {
receive();
} else {
send();
}
}
}
So let's run it with Terracotta and see how it works. First, we need to "cluster" the app. We need the
lock
and queue
objects to be the same cluster-wide, which in Terracotta is called a root. So our Terracotta configuration file looks like:<tc:tc-config xmlns:tc="http://www.terracotta.org/config"Now, let's run two JVMs with Terracotta. First, we start a server instance:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-4.xsd">
<application>
<dso>
<roots>
<root>
<field-name>SimpleMessage.lock</field-name>
</root>
<root>
<field-name>SimpleMessage.queue</field-name>
</root>
</roots>
</dso>
</application>
</tc:tc-config>
$ start-tc-server.shThen, we start our JVMs.
2008-12-14 10:26:18,246 INFO - Terracotta Server has started up as ACTIVE node on
0.0.0.0:9510 successfully, and is now ready for work.
JVM 1:
$ dso-java.sh SimpleMessageJVM 2:
Receiving Messages...
$ dso-java.sh SimpleMessageHere, we enter a message, and see that it is printed in JVM 1:
Enter a message>
JVM 2:
$ dso-java.sh SimpleMessageJVM 1:
Enter a message> hello world
$ dso-java.sh SimpleMessageFurther exploration
Receiving Messages...
msg >> hello world
Try starting another JVM and see that they can both send messages to JVM 1. Try killing the receiver JVM and send messages to it. Then start another JVM. Since the lock is no longer held (Terracotta automatically releases any locks held by a JVM that exits the cluster) the new JVM will take on the receiver mode. Any messages sent while there was no receiver will have been queued, and will be printed on the startup of this new node.
And of course, you can see all the activity in the cluster. Try taking the receiver down again, send some messages using the sender nodes, then run the admin console. You'll be able to inspect the messages in the queue using the clustered heap browser.
This is just a demonstration of course - so to keep it simple I used a
String
as the message - but you could use any class.For more fun with Terracotta, try the helpful "recipes" at Terracotta.org.
(Note, I've blogged about simple coordination in the past using Terracotta, which is similar)
12 comments:
Why not to use eventBus ?
https://eventbus.dev.java.net/
@Oren
Unless I am mistaken it seems EventBus is designed for single-process messaging.
The example I listed is for multi process messaging.
Bought the Tennis Racquet is important, exercise can reduce the harm, especially for the wrist injury. But the good of Tennis Racket in general in the real prices are more expensive, so a lot of websites now have cheap tennis racquet、tennis racquet discount、cheap tennis racket、discount Tennis Racket .
There are ed hardy shirts
,pretty ed hardy shirt for men, ed hardy womens in the ed hardy online store designed by ed hardy ,many cheap ed hardy shirt ,glasses,caps,trouers ed hardy shirts on sale ,
You can go to edhardyshirts.com to have a look ,you may find one of ed hardy clothing fit for you
http://straighthairgirl.blog126.fc2.com
http://www.seriousblogging.com/crazygirlsshirts
http://www.free-blog-site.com/iammyself
http://d.hatena.ne.jp/hotfishing
puma mens shoes
nike air max ltd
NIKE air shoes
nike max ltd
orange converse
adidas shoes
nike shoes
puma shoes
addidas shoes
cheap converse shoes
cheap nike shoes
cheap puma shoes
I like your blog. Thank you. They are really great . Ermunterung ++ .
Some new style Puma Speed is in fashion this year.
chaussure puma is Puma shoes in french . Many Franzose like seach “chaussure sport” by the internet when they need buy the Puma Shoes Or nike max shoes. The information age is really convenient .
By the way ,the nike max ltd is really good NIKE air shoes ,don’t forget buy the puma mens shoes and nike air max ltd by the internet when you need them . Do you know Nike Air Shoes is a best Air Shoes . another kinds of Nike shoes is better . For example , Nike Air Rift is good and Cheap Nike Shoes .the nike shox shoes is fitting to running.
Spring is coming, Do you think this season is not for Ugg Boots? maybe yes .but this season is best time that can buy the cheap ugg boots. Many sellers are selling discounted. Do not miss . Please view my fc2 blog and hair straighteners blog.
.thank you .
I like orange converse shoes ,I like to buy the cheap converse shoes by the internet shop . the puma shoes and the adidas shoes (or addidas shoes) are more on internet shop .i can buy the cheap nike shoes and cheap puma shoes online. It’s really convenient.
Many persons more like Puma basket shoes than nike air rift shoes . the Puma Cat shoes is a kind of Cheap Puma Shoes .
If you want to buy the Cheap Nike Air shoes ,you can buy them online. They are same as the Nike Air shoes authorized shop. Very high-caliber Air shoes and puma cat shoes . the cheap puma shoes as same as other.
I like your blog . They are really great. Ermunterung ++ .please pay a return visit to my blog .thank you.
http://www.soulcast.com/iblog99/
http://iblog99.jugem.jp
http://iblog99.cocolog-nifty.com/blog/
ugg boots,
cheap ugg boots,
discount ugg boots,
australia ugg boots,
wholesale ugg boots,
sheepskin ugg boots,
ugg boots 5815,
ugg boots size 9,
ugg snowboots,
womens ugg boots,
classic tall ugg boots ,
short ugg boots,
new ugg boots,
winter ugg boots,
jordan 18
jordan 19
jordan 1
jordan 2
jordan 3
jordan 4
jordan 5
jumpmen23
Wow, this is too cool. I am very like it, Thank you for sharing, let me so happy! At the same time ,i feel Forged Steel Valve
Java Service Wrapper - What is the Java Service Wrapper Nike shox
Helo, your blog is really good, I like it very much!By the way, if you like nike chaussures tn you can come here to have a look!
ugg outlet
uggs outlet
ugg classic
ugg classic boots
ugg online
ugg boots
cheap ugg boots
discount ugg boots
australia ugg boots
wholesale ugg boots
sheepskin ugg boots
ugg boots 5815
ugg boots size 9
ugg snow boots
womens ugg boots
classic tall ugg boots
short ugg boots
new ugg boots
winter ugg boots
ugg boots outlet
Post a Comment