Implementing Message Sending and Reception
The following lines of code from the sender application show how to define and send a simple text message.
public void sendAMessage(String messageText)
{
...
TextMessage msg = session.createTextMessage();
msg.setText(messageText);
sender.send(msg);
The following code block from the receiver application shows an implementation for receiving a simple text message:
Message msg = receiver.receive();
if (msg instanceof TextMessage) {
System.out.println("Received message: " +
((TextMessage) msg).getText());
} else {
System.out.println("Received message: " + msg.toString());
}
The receive() method in this sample application receives one message at a time (that is, a single message each time it is called). In a production application, you would typically call receive() from within a loop so it is able to receive messages continuously.