What is messaging?

What is messaging in Java? If possible, provide an example.

+6
java
source share
3 answers

Messaging in Java

  • When a thread sends a message (object) to another thread.

  • Used to communicate threads and synchronize in environments where threads do not have shared memory. Therefore, threads cannot share semaphores or monitors and cannot use shared variables for communication. Messaging can still be used, of course, on a platform with shared memory.

  • Messages are sent over a channel with an operation such as transmission (channel, message) and are received from the channel with an operation such as reception (channel, message). Messages can be transmitted synchronously, which means that the sender blocks until the received receiver receives and the receiver, until the sender sends. Since the sender and the recipient are at certain known points of their code at a known specific point in time, synchronous messaging is also called a simple rendezvous with a one-way flow of information from the sender to the recipient. An example is a chess game agent. Agents can process messages synchronously, as they will be acknowledged throughout the game.

  • When transmitting an asynchronous message, the sender is not blocked. If the receiver does not expect to receive the message, the message is queued or buffered. The receiver is still blocked if there is no message in the queue or buffering while receiving.

+15
source share

The classic interaction between two streams: producer and consumer.

import java.util.Vector; class Producer extends Thread { static final int MAXQUEUE = 5; private Vector messages = new Vector(); public void run() { try { while ( true ) { putMessage(); sleep( 1000 ); } } catch( InterruptedException e ) { } } private synchronized void putMessage() throws InterruptedException { while ( messages.size() == MAXQUEUE ) wait(); messages.addElement( new java.util.Date().toString() ); notify(); } // Called by Consumer public synchronized String getMessage() throws InterruptedException { notify(); while ( messages.size() == 0 ) wait(); String message = (String)messages.firstElement(); messages.removeElement( message ); return message; } } class Consumer extends Thread { Producer producer; Consumer(Producer p) { producer = p; } public void run() { try { while ( true ) { String message = producer.getMessage(); System.out.println("Got message: " + message); sleep( 2000 ); } } catch( InterruptedException e ) { } } public static void main(String args[]) { Producer producer = new Producer(); producer.start(); new Consumer( producer ).start(); } } 
+12
source share

Your question is a bit vague, but I think you can reference the Java Message Service API? If so, Wikipedia can tell you everything: http://en.wikipedia.org/wiki/Java_Message_Service

But if you are talking about a more "general" message, then I suggest you take a look at the ewernli link published!

-one
source share

All Articles