LISTING 1
public class InvalidateCacheCommand implements Command {
private final static Map GLOBAL_CACHE = new HashMap();
private final String key;
public InvalidateCacheCommand(String key) {
if (key == null) {
throw new IllegalArgumentException("Key is null.");
}
this.key = key;
}
public void execute() {
System.out.println("Clearing cache for key: " + key);
GLOBAL_CACHE.remove(key);
}
}
LISTING 2
// NOTE: This class uses undocumented private WebLogic APIs not guaranteed to be supported
by future versions of WebLogic. Use at your own risk.
public final class ClusterNotifier {
private static MulticastSession session;
private ClusterNotifier() {}
public static void notify(Command command) {
if (command == null) {
throw new IllegalArgumentException("Command is null.");
}
if (session == null) {
System.out.println("Not running in a cluster. Invoking command locally..");
command.execute();
}
else
{
System.out.println("Running in a cluster. Sending message to cluster
members..");
try
{
session.send(new DelegateMessage(command));
} catch (IOException io) {
io.printStackTrace();
System.err.println("An IO Exception occurred while sending the " +
"message to the cluster." + io.getMessage());
}
}
}
public static void main(String argv[]) {
Command command = new InvalidateCacheCommand("TestKey");
ClusterNotifier.notify(command);
}
private static synchronized MulticastSession getSession()
{
if(session == null)
{
ClusterServices cluster = ClusterService.getServices();
if(cluster == null)
return null;
session = cluster.createMulticastSession(null, -1, false);
}
return session;
}
public static class DelegateMessage implements GroupMessage {
private final Command command;
public DelegateMessage() {
this(null);
}
public DelegateMessage(Command command) {
this.command = command;
}
public void execute(HostID ignoredHostID) {
if (command != null) {
command.execute();
}
}
}
}