| |
"Power JMS, by Tarak Modi"
Vol. 6, Issue 5, p. 34
Listing 1
// create a new URL with our custom "jms" protocol.
URL url = new URL("jms://Queue/ModiQueue");
URLConnection uc = url.openConnection();
// Send a message
DataOutputStream dos =
new DataOutputStream(uc.getOutputStream());
dos.writeUTF("Hello!");
dos.flush();
// Receive the message
DataInputStream dis = new DataInputStream(uc.getInputStream());
String message = dis.readUTF();
// close the streams.
dos.close();
dis.close();
Listing 2
// Get the list of package prefixes
// protocolPathProp has been defined as
// "java.protocol.handler.pkgs"
String packagePrefixList = null;
PackagePrefixList = (String)
java.security.AccessController.doPrivileged(
new sun.security.action.GetPropertyAction(
protocolPathProp,""));
// Add the standard protocols package to the list!
// First, if any package prefixes were found, append
// another delimiter, "|", to the end.
if (packagePrefixList != "") {
packagePrefixList += "|";
}
// and now append "sun.net.www.protocol" to the end.
// Important:
// Since this package is appended at the end of user
// specified packages, a user can override any of the
// Sun provided protocol handler implementations, such
// as the one for http.
packagePrefixList += "sun.net.www.protocol";
// And now parse through the list...
// Remember, "|" is the delimiter.
StringTokenizer packagePrefixIter =
new StringTokenizer(packagePrefixList, "|");
// Keep going until either we get a handler or
// no more tokens remain.
// Note that there will always be at least
// one token, sun.net.www.protocol.
while (handler == null && packagePrefixIter.hasMoreTokens()) {
// Get the next token
String packagePrefix =
packagePrefixIter.nextToken().trim();
try {
// Create the fully qualified class name.
// Eg. jmsbook + jms + ".Handler"
String clsName = packagePrefix + "." +
protocol + ".Handler";
Class cls = null;
try {
// Now try loading the class with that name.
clsClass.forName(clsName);
}
catch (ClassNotFoundException e) {
ClassLoader cl =
ClassLoader.getSystemClassLoader();
if (cl != null) {
cls = cl.loadClass(clsName);
}
}
if (cls != null) {
// create a new instance.
handler = (URLStreamHandler)cls.newInstance();
}
}
catch (Exception e) {
// any number of exceptions can get thrown here
// move onto the next token...
}
} // while loop.
Listing 3
// This is the static hash table used
// to cache the protocol handlers.
// All access to this table must be synchronized.
static Hashtable handlers = new Hashtable();
static synchronized URLStreamHandler getURLStreamHandler(
String protocol) {
// Have we already resolved this protocol?
URLStreamHandler handler =
(URLStreamHandler)handlers.get(protocol);
// Maybe not...
if (handler == null) {
// Use the factory (if any)
// We will not consider this case.
// In a nutshell, a factory implements the
// URLStreamHandlerFactory interface and is
// registered with the URL instance either during
// construction or using the
// setURLStreamHandlerFactory method.
// A factory can only be set once and similar to the
// protocol handlers is shared by all URL instances.
if (factory != null) {
handler =
factory.createURLStreamHandler(protocol);
}
// still don't have a handler...
if (handler == null) {
// All the logic to
resolve a protocol
// string to a protocol handler.
// Plug in the implementation that
// we saw above here
}
// Cache the handler if one was found.
if (handler != null) {
handlers.put(protocol, handler);
}
}
// Return the handler to the caller.
return handler;
}
Listing 4
// uc is a JmsURLConnection
// Wrap/Decorate the JmsOutputStream with a DataOutputStream
DataOutpputStream dos =
new DataOutputStream(uc.getOutputStream());
// Write the name (string), sex (string)
// and age (long) to the stream.
dos.writeUTF(name);
dos.writeUTF(sex);
dos.writeLong(age);
// send the message.
dos.flush();
// done
dos.close();
Listing 5
// uc is a JmsURLConnection
// Wrap/Decorate the JmsInputStream with a DataInputStream
DataInpputStream dis =
new DataInputStream(uc.getInputStream());
// Read the name (string), sex (string)
// and age (long) from the stream.
String name = dos.readUTF(name);
String sex = dos.readUTF(sex);
Long age = dos.readLong(age);
// done
dis.close();
|
|