|
/*
* Created on Jun 3, 2005
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URI;
import com.safmq.MQBuilder;
import com.safmq.MQException;
import com.safmq.MessageQueue;
import com.safmq.QueueMessage;
import com.safmq.Safmq;
/**
* Note: this example expects a SAFMQ server to be running on the "localhost"
* or same computer as the example is being run. It also expects a user to exist
* with the name "username" and password "password". Additionally there should
* be two queues "query" and "resposne", and the user "username" should have
* read and write access to to this queue.
*
* @author matt
*/
public class RoundTripServer {
static URI queue_name;
static String user_name = "username";
static String password = "password";
static {
try {
queue_name = new URI("safmq://localhost/query");
} catch (Exception e) {
}
}
public static void main(String[] args) {
try {
// Allocate a connection to the query queue, we'll read from this queue
MessageQueue mq = MQBuilder.buildMessageQueue(queue_name,user_name,password);
// Allocate a messaeg so we can read from it.
QueueMessage msg = new QueueMessage();
int error;
// Try and retreive a message from the queue
error = mq.Retreive(true,-1,msg);
// Close the queue we don't need it any more, this release resources on the server
mq.Close();
if (error == Safmq.EC_NOERROR) {
// Output the contents of the emssage
System.out.println("Message Data Follows");
System.out.println("Label: " + msg.getLabel());
InputStream in = msg.getInputStream();
byte data[] = new byte[1024];
int read;
while ( (read=in.read(data)) > 0) {
System.out.write(data,0,read);
}
// allocate a response message
QueueMessage response = new QueueMessage();
// Set the message's body type to give readers a clue to contents
response.setBodyType(Safmq.BT_TEXT);
// Set a name for the message, note this is optional, but some receivers
// may choose an action depending on the name specified here
response.setLabel("Round Trip Response");
// Note the next line is the critical step, it ties the response message
// with the original query message.
response.setReciptID(msg.getMessageID());
// Get the output stream to fill the body.
// In this case wrap it with a PrintWriter so that we can have formated output.
PrintWriter pw = new PrintWriter(new OutputStreamWriter(response.getOutputStream()));
pw.println("This is the response to the round trip client's query");
pw.flush();
// Connect to the response queue
MessageQueue responseQueue = MQBuilder.buildMessageQueue(new URI(msg.getResponseQueueName()),user_name,password);
error = responseQueue.Enqueue(response);
responseQueue.Close();
if (error != Safmq.EC_NOERROR)
System.out.println("Error sending: "+Safmq.errorDecode(error));
} else {
System.out.println("Error retreiving: "+Safmq.errorDecode(error));
}
} catch (MQException mqe) {
mqe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
A programmer for 20 years and professionaly employed for 12, I am currently Cheif Engineer for Pharmacy Chare Professionals, Inc., located in Omaha, NE.
My experience is in the area of OO Design, Application, and programming, technical team leadership, RDBMS applications, ISAM applications, Image Processing, Mathematical image generation, Client-Server business applications, eBusiness applications, XML & EDI B2B communications, Java application development, C/C++ application development, CFML/ASP/VB development, on systems like Win2K/NT/98/95, Linux, Irix, Solaris, and MacOS.