ejb dom
dennis Jan 30, 2002 11:40 PMWriting a MBean is a lot more simple than writing a EJB ;)
Simply write a class and its MBean interface and it starts. Look at ... all Jboss to find example.
I don't use cluster and don't know exactly how to handle this problem in JMX. (BTW is the marc/juha book on JMX out ?). For the moment I would simply deploy my MBean in all the servers I am running. For example I use 2 servers at devel time on for tomcat 3.2 and one for ejb, ... Both runs the MBean.
I could use the RMI connector (I tries it without problems) but it is a jboss specific so I prefer not using it anymore.
What took me long time to catch is the fact that MBean is not just there for JBoss. It is also there for a lot of configuration issue that users face in any project. IMHO, using env definition in ejb is less and less a good option for "configuration" stuff.
Example :
public class EISIntegratorLoader
extends FileLoader
implements EISIntegratorLoaderMBean{
// ----------------------------------------------------------------- Logging
transient static Category cat=Category.getInstance(EISIntegratorLoader.class);
private String fileName;
private boolean started;
private Map mapping = new Hashtable();
public String getName(){
if (cat.isDebugEnabled()) cat.debug("getName()");
return "SecureActionLoader";
}
public String getConfigFile(){
if (cat.isDebugEnabled()) cat.debug("getConfigFile()");
return fileName;
}
public void setConfigFile(String fileName)
throws FileNotFoundException{
if (cat.isDebugEnabled()) cat.debug("setConfigFile("+fileName+")");
this.fileName = fileName;
Document doc = extractDoc(findFile(fileName));
List list = doc.getRootElement().getChildren("mapping");
//List list = doc.getMixedContent();
if (cat.isDebugEnabled()) cat.debug("MAPPING Number = "+list.size());
Iterator i = list.iterator();
while (i.hasNext()){
Element el = (Element)i.next();
String source = el.getChildText("source");
String destination = el.getChildText("destination");
if (!mapping.containsKey(source)){
mapping.put(source,destination);
if (cat.isDebugEnabled()) cat.debug("MAPPING : "+source+" ==> "+destination);
}
}
}
public Map getMapping(){
if (cat.isDebugEnabled()) cat.debug("getMapping");
return this.mapping;
}
public void start() throws Exception{
cat.info("start()");
started = true;
}
public void stop(){
cat.info("stop()");
started = false;
}
// ----------------------------------------------------------------- Private
/** This will build the Document object
* @param url the URL for the XML configuration file
* @return a {@link Document} representing the actions
* definition
*/
private Document extractDoc(URL url){
if (cat.isDebugEnabled()) cat.debug("Executing extractDoc");
SAXBuilder builder = new SAXBuilder();
if (cat.isDebugEnabled()) cat.debug("Builder="+builder);
// build the document
Document doc;
try{
doc = builder.build(url);
if (cat.isDebugEnabled()) cat.debug("Doc="+doc);
} catch (Exception e){
cat.debug("Exception",e);
throw new IllegalStateException("JDOMException"+e.getMessage());
}
Document actionDoc = new Document(doc.getRootElement().getChild("mappings"));
return actionDoc;
}
The interface is
public String getConfigFile();
public void setConfigFile(String fileName) throws FileNotFoundException;
public Document getDocument();
public void start() throws Exception;
public void stop() throws Exception;
With FileLoader being
protected URL findFile(String fileName) throws IllegalStateException {
Class confClass = this.getClass();
ClassLoader loader = confClass.getClassLoader();
URL fileURL = loader.getResource(fileName);
// Was the resource found?
if (fileURL == null) {
cat.error("Can't find the file: '" + fileName + "'");
throw new IllegalStateException("Can't locate file: '" + fileName + "'");
}
else {
return fileURL;
}
}
The call is done like this (in j2ee pattern singleton ServiceLocator) :
private ServiceLocator() {
if (cat.isDebugEnabled()) cat.debug("Constructor");
try{
if (useCache) cache = new Hashtable();
// Get the JNDI Initial Context (local and remote)
initial = new InitialContext();
if (cat.isDebugEnabled()) cat.debug("InitialContext="+initial);
// Get the Deployment Description
ArrayList servers = MBeanServerFactory.findMBeanServer(null);
if (cat.isDebugEnabled()) cat.debug("List of MBean Server = " + servers);
if (servers == null || servers.size() == 0) {
// Try to connect remotely (TestSecAConfigurator will do that)
// Remove it for jboss 3.0 compatibility
//JMXConnector server = new RMIClientConnectorImpl(InetAddress.getLocalHost().getHostName());
//ObjectName objName = new ObjectName("HubMethods:service=EISIntegratorLoader");
//mapping = (Map) server.getAttribute(objName, "Mapping");
mapping = new FastHashMap();
} else {
try{
MBeanServer server = (MBeanServer) servers.get(0);
ObjectName objName = new ObjectName("HubMethods:service=EISIntegratorLoader");
mapping = (Map) server.getAttribute(objName, "Mapping");
}catch (Exception e){
cat.warn("EISIntegratorLoader MBean could not be accessed "+e.getMessage());
mapping = new FastHashMap();
}
}
if (cat.isDebugEnabled()) cat.debug("Mapping Size : "+mapping.size());
}catch (Exception e){
cat.error("Exception",e);
}
}