Implementing Messaging on a Second Thread
In addition to receiving customer inquiry messages on an asynchronous messaging thread, the server application also receives administrator shutdown commands on the main thread.
The administrator shutdown command is sent from an admin client to the server. To control which clients can send the shutdown command to the server, you can create a separate client group that has permission to send messages to the admin Queue. You use the JMSAdmin command-line tool to create this group at server application and admin client startup.
The setup code for the shutdown command contains its own set of messaging objects (session, message consumer, and destination). The setup and initialization (factory creation and lookup) code portions are similar to those of the sender-receiver application in
A Basic JMS Sender-Receiver Client.
Session adminSession;
MessageConsumer adminReceiver;
Destination adminDestination;
...
adminSession = conn.createSession(false,
Session.AUTO_ACKNOWLEDGE);
adminReceiver = adminSession.createConsumer(adminDestination);
A loop in serverRun() control server operation and shutdown, as shown below. Message reception for the shutdown command occurs via a synchronous receive() method, unlike the asynchronous message listener that receives customer inquiries on the other messaging thread.
void serverRun()
{
boolean running = true;
while (running) {
try {
Message msg = adminReceiver.receive();
String command =
msg.getStringProperty(AppConstants.ADMIN_CMD_PROP);
... }
if (command.equals(AppConstants.ADMIN_CMD_SHUTDOWN)) {
conn.stop();
session.setMessageListener(null);
running = false;
}
...
}