Create a WorkManager Service Using the CommomJ Library
srose35 Apr 17, 2012 12:08 PMI am attempting to port the jbosscommonj-1.0 library to use jboss as7.
The biggest issue so far is that I cant find a way to easily define a service.
I am using the code from the Original jbosscommonj Work Manager Service but changing the way I am doing the JNDI Binding so I can do a look up of the Work Manager and it will work like it did.
Here is where I am so Far. I am not sure this will even work . right now I am trying to figure out how to get access to the ServiceActivatorContext so I can call activate. Your help will be greatly appreciated. Thanks
package net.sf.jbosscommonj.workmanager;
import commonj.work.WorkManager;
import de.myfoo.commonj.util.ThreadPool;
import de.myfoo.commonj.work.FooWorkManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.as.naming.ManagedReference;
import org.jboss.as.naming.ManagedReferenceFactory;
import org.jboss.as.naming.ServiceBasedNamingStore;
import org.jboss.as.naming.deployment.ContextNames;
import org.jboss.as.naming.service.BinderService;
import org.jboss.modules.ModuleClassLoader;
import org.jboss.msc.service.*;
import org.jboss.system.ServiceMBeanSupport;
import org.jboss.util.naming.NonSerializableFactory;
/**
*
* @author <a href="mailto:davideconsonni@users.sourceforge.net">Davide Consonni</a>
*/
public class WorkManagerService implements WorkManagerServiceMBean,ServiceActivator {
public static Log logger = LogFactory.getLog(WorkManagerService.class.getName());
private int minThreads;
private int maxThreads;
private int queueLength;
private String JNDIName;
private WorkManager workmanager;
private String MY_GLOBAL_BINDING_NAME = "WorkManager";
private String MY_APP_BINDING_NAME = "WorkManager";
// protected void start() throws Exception {
// ThreadPool pool = new ThreadPool(minThreads, maxThreads, queueLength);
// workmanager = new FooWorkManager(pool);
// bind(workmanager, JNDIName);
// }
//
// protected void stop() throws Exception {
// unbind(JNDIName);
//
// }
/**
* Mininum number of threads
* @return the minThreads
*/
public int getMinThreads() {
return minThreads;
}
/**
* Mininum number of threads
* @param minThreads the minThreads to set
*/
public void setMinThreads(int minThreads) {
this.minThreads = minThreads;
}
/**
* Maximum number of threads
* @return the maxThreads
*/
public int getMaxThreads() {
return maxThreads;
}
/**
* Maximum number of threads
* @param maxThreads the maxThreads to set
*/
public void setMaxThreads(int maxThreads) {
this.maxThreads = maxThreads;
}
/**
* Length of the execution queue
* @return the queueLength
*/
public int getQueueLength() {
return queueLength;
}
/**
* Length of the execution queue
* @param queueLength the queueLength to set
*/
public void setQueueLength(int queueLength) {
this.queueLength = queueLength;
}
/**
* JNDI Binding
* @return the JNDIName
*/
public String getJNDIName() {
return JNDIName;
}
/**
* JNDI Binding
* @param JNDIName the JNDIName to set
*/
public void setJNDIName(String JNDIName) {
this.JNDIName = JNDIName;
}
private class WorkerManagedReferenceFactory implements ManagedReferenceFactory {
private final WorkManager wm;
public WorkerManagedReferenceFactory(final WorkManager wm) {
ThreadPool pool = new ThreadPool(minThreads, maxThreads, queueLength);
this.wm = wm;
}
public ManagedReference getReference() {
return new ManagedReference() {
public void release() {
}
public Object getInstance() {
return wm;
}
};
}
}
public void activate(final ServiceActivatorContext serviceActivatorContext) throws ServiceRegistryException {
System.out.println("ACTIVATE");
ModuleClassLoader loader = (ModuleClassLoader) getClass().getClassLoader();
final String archiveName = loader.getModule().getIdentifier().getName().substring("deployment.".length());
final String applicationName = archiveName.substring(0, archiveName.length() - 4);
ThreadPool pool = new ThreadPool(minThreads, maxThreads, queueLength);
workmanager = new FooWorkManager(pool);
// //create a global binding
final ServiceName bindingServiceName = ContextNames.GLOBAL_CONTEXT_SERVICE_NAME.append(MY_GLOBAL_BINDING_NAME);
final BinderService binderService = new BinderService(MY_GLOBAL_BINDING_NAME);
ServiceBuilder<ManagedReferenceFactory> builder = serviceActivatorContext.getServiceTarget().addService(bindingServiceName, binderService);
builder.addDependency(ContextNames.GLOBAL_CONTEXT_SERVICE_NAME, ServiceBasedNamingStore.class, binderService.getNamingStoreInjector());
binderService.getManagedObjectInjector().inject(new WorkerManagedReferenceFactory(workmanager));
builder.install();
final ServiceName appBindingServiceName = ContextNames.bindInfoFor("jndi-example", "jndi-example", "jndi-example", "java:app/" + MY_APP_BINDING_NAME).getBinderServiceName();
final BinderService appBinderService = new BinderService(MY_APP_BINDING_NAME);
builder = serviceActivatorContext.getServiceTarget().addService(appBindingServiceName, appBinderService);
builder.addDependency(ContextNames.contextServiceNameOfApplication(applicationName), ServiceBasedNamingStore.class, appBinderService.getNamingStoreInjector());
appBinderService.getManagedObjectInjector().inject(new WorkerManagedReferenceFactory(workmanager));
builder.install();
}
// private void bind(Object object, String bindName) throws NamingException {
// // Ah ! Session isn't serializable, so we use a helper class
// NonSerializableFactory.bind(bindName, object);
// Context ctx = new InitialContext();
// try {
// Name n = ctx.getNameParser("").parse(bindName);
// while (n.size() > 1) {
// String ctxName = n.get(0);
// try {
// ctx = (Context) ctx.lookup(ctxName);
// } catch (NameNotFoundException e) {
// ctx = ctx.createSubcontext(ctxName);
// }
// n = n.getSuffix(1);
// }
// StringRefAddr addr = new StringRefAddr("nns", bindName);
// Reference ref = new Reference(object.getClass().getName(), addr, NonSerializableFactory.class.getName(), null);
// ctx.bind(n.get(0), ref);
// } finally {
// ctx.close();
// }
// // log.info(serviceName.getDomain() + " Service bound to " + bindName);
// }
//
// private void unbind(String bindName) throws NamingException {
// if (bindName != null) {
// InitialContext ctx = new InitialContext();
// try {
// ctx.unbind(bindName);
// } finally {
// ctx.close();
// }
// NonSerializableFactory.unbind(bindName);
// // log.info("Service '" + serviceName.getDomain() + "' removed from JNDI");
// }
// }
}