IDEMPIERE-3653 1004207 Change behavior of system cacheing for multi-tenants. Add API to support dynamic addition and removal of scheduler.

This commit is contained in:
Heng Sin Low 2014-08-19 17:16:01 +08:00
parent e682565d95
commit 4cfe1258b0
9 changed files with 138 additions and 8 deletions

View File

@ -32,7 +32,7 @@ public class ReplicationServerFactory implements IServerFactory<ReplicationProce
MIMPProcessor[] importModels = MIMPProcessor.getActive(ctx);
for (MIMPProcessor lp : importModels)
{
ReplicationProcessor server = new ReplicationProcessor(lp);
ReplicationProcessor server = create(ctx, lp);
list.add(server);
}
ReplicationProcessor[] servers = list.toArray(new ReplicationProcessor[0]);
@ -44,4 +44,9 @@ public class ReplicationServerFactory implements IServerFactory<ReplicationProce
return MIMPProcessor.class;
}
@Override
public ReplicationProcessor create(Properties ctx, MIMPProcessor serverModel) {
return new ReplicationProcessor(serverModel);
}
}

View File

@ -31,6 +31,14 @@ public interface IServerFactory<S extends AdempiereServer, M extends AdempierePr
*/
public S[] create (Properties ctx);
/**
*
* @param ctx
* @param serverModel
* @return server
*/
public S create(Properties ctx, M serverModel);
/**
* The Adempiere Server Manager will used this to avoid running duplicate server for the same
* AdempiereProcessor model.

View File

@ -31,6 +31,7 @@ import org.adempiere.server.AdempiereServerActivator;
import org.adempiere.server.IServerFactory;
import org.compiere.Adempiere;
import org.compiere.model.AdempiereProcessor;
import org.compiere.model.MScheduler;
import org.compiere.model.MSession;
import org.compiere.util.CLogger;
import org.compiere.util.Env;
@ -50,13 +51,26 @@ public class AdempiereServerMgr implements ServiceTrackerCustomizer<IServerFacto
{
private static ServiceTracker<IServerFactory<AdempiereServer, AdempiereProcessor>, IServerFactory<AdempiereServer, AdempiereProcessor>> serviceTracker;
public static int SERVER_STATE_NOT_SCHEDULE = 0;
public static int SERVER_STATE_STARTED = 1;
public static int SERVER_STATE_STOPPED = 2;
/**
* Get Adempiere Server Manager
* @return mgr
*/
public synchronized static AdempiereServerMgr get()
{
if (m_serverMgr == null)
return get(true);
}
/**
* Get Adempiere Server Manager
* @return mgr
*/
public synchronized static AdempiereServerMgr get(boolean createNew)
{
if (m_serverMgr == null && createNew)
{
m_serverMgr = new AdempiereServerMgr();
serviceTracker = new ServiceTracker<IServerFactory<AdempiereServer, AdempiereProcessor>, IServerFactory<AdempiereServer, AdempiereProcessor>>(AdempiereServerActivator.getBundleContext(),
@ -157,6 +171,34 @@ public class AdempiereServerMgr implements ServiceTrackerCustomizer<IServerFacto
}
}
}
/**
* @param scheduler
* @return true
*/
public boolean addScheduler(MScheduler scheduler) {
String serverId = scheduler.getServerID();
if (getServer(serverId) != null)
return false;
//osgi server
List<IServerFactory> serverFactoryList = Service.locator().list(IServerFactory.class).getServices();
if (serverFactoryList != null && !serverFactoryList.isEmpty())
{
for(IServerFactory factory : serverFactoryList )
{
if (factory.getProcessorClass().getName().equals(scheduler.getClass().getName())) {
AdempiereServer server = factory.create(m_ctx, scheduler);
if (server != null && AdempiereServer.isOKtoRunOnIP(scheduler)) {
m_servers.add(new ServerWrapper(server));
return start(serverId);
}
}
}
}
return false;
}
/**
* Get Server Context
@ -413,6 +455,28 @@ public class AdempiereServerMgr implements ServiceTrackerCustomizer<IServerFacto
return retValue;
} // getAll
public synchronized int getStatus(AdempiereProcessor processor)
{
int status = SERVER_STATE_NOT_SCHEDULE;
for (int i = 0; i < m_servers.size(); i++)
{
ServerWrapper server = m_servers.get(i);
AdempiereProcessor model = server.server.getModel();
if (model.getClass().getName().equals(processor.getClass().getName()) && model.getServerID().equals(processor.getServerID()))
{
if (server.scheduleFuture == null || server.scheduleFuture.isDone())
{
status = SERVER_STATE_STOPPED;
}
else
{
status = SERVER_STATE_STARTED;
}
}
}
return status;
}
/**
* Get Server with ID
* @param serverID server id
@ -556,4 +620,26 @@ public class AdempiereServerMgr implements ServiceTrackerCustomizer<IServerFacto
serviceTracker.open();
}
}
public Boolean remove(String serverID) {
ServerWrapper server = getServer(serverID);
if (server == null)
return false;
if (server.scheduleFuture != null && !server.scheduleFuture.isDone()) {
if (!stop(serverID)) {
return false;
}
}
for (int i = 0; i < m_servers.size(); i++) {
server = m_servers.get(i);
if (serverID.equals(server.server.getServerID())) {
m_servers.remove(i);
return true;
}
}
return false;
}
} // AdempiereServerMgr

View File

@ -42,7 +42,7 @@ public class DefaultAcctProcessorFactory implements IServerFactory<AcctProcessor
List<AcctProcessor> list = new ArrayList<AcctProcessor>();
for (MAcctProcessor pModel : acctModels)
{
AcctProcessor processor = new AcctProcessor(pModel);
AcctProcessor processor = create(ctx, pModel);
list.add(processor);
}
return list.toArray(new AcctProcessor[0]);
@ -53,4 +53,9 @@ public class DefaultAcctProcessorFactory implements IServerFactory<AcctProcessor
return MAcctProcessor.class;
}
@Override
public AcctProcessor create(Properties ctx, MAcctProcessor serverModel) {
return new AcctProcessor(serverModel);
}
}

View File

@ -39,7 +39,7 @@ public class DefaultAlertProcessorFactory implements IServerFactory<AlertProcess
List<AlertProcessor> list = new ArrayList<AlertProcessor>();
for (MAlertProcessor pModel : alertModels)
{
AlertProcessor server = new AlertProcessor(pModel);
AlertProcessor server = create(ctx, pModel);
list.add(server);
}
return list.toArray(new AlertProcessor[0]);
@ -50,4 +50,9 @@ public class DefaultAlertProcessorFactory implements IServerFactory<AlertProcess
return MAlertProcessor.class;
}
@Override
public AlertProcessor create(Properties ctx, MAlertProcessor serverModel) {
return new AlertProcessor(serverModel);
}
}

View File

@ -39,7 +39,7 @@ public class DefaultLDAPProcessorFactory implements IServerFactory<LdapProcessor
List<LdapProcessor> list = new ArrayList<LdapProcessor>();
for (MLdapProcessor lp : ldapModels)
{
LdapProcessor server = new LdapProcessor(lp);
LdapProcessor server = create(ctx, lp);
list.add(server);
}
return list.toArray(new LdapProcessor[0]);
@ -49,4 +49,9 @@ public class DefaultLDAPProcessorFactory implements IServerFactory<LdapProcessor
public Class<MLdapProcessor> getProcessorClass() {
return MLdapProcessor.class;
}
@Override
public LdapProcessor create(Properties ctx, MLdapProcessor serverModel) {
return new LdapProcessor(serverModel);
}
}

View File

@ -42,7 +42,7 @@ public class DefaultRequestProcessorFactory implements IServerFactory<RequestPro
List<RequestProcessor> list = new ArrayList<RequestProcessor>();
for (MRequestProcessor pModel : requestModels)
{
RequestProcessor processor = new RequestProcessor(pModel);
RequestProcessor processor = create(ctx, pModel);
list.add(processor);
}
return list.toArray(new RequestProcessor[0]);
@ -52,4 +52,9 @@ public class DefaultRequestProcessorFactory implements IServerFactory<RequestPro
public Class<MRequestProcessor> getProcessorClass() {
return MRequestProcessor.class;
}
@Override
public RequestProcessor create(Properties ctx, MRequestProcessor serverModel) {
return new RequestProcessor(serverModel);
}
}

View File

@ -39,7 +39,7 @@ public class DefaultSchedulerFactory implements IServerFactory<Scheduler, MSched
List<Scheduler> list = new ArrayList<Scheduler>();
for (MScheduler pModel : schedulerModels)
{
Scheduler server = new Scheduler(pModel);
Scheduler server = create(ctx, pModel);
list.add(server);
}
return list.toArray(new Scheduler[0]);
@ -50,4 +50,9 @@ public class DefaultSchedulerFactory implements IServerFactory<Scheduler, MSched
return MScheduler.class;
}
@Override
public Scheduler create(Properties ctx, MScheduler serverModel) {
return new Scheduler(serverModel);
}
}

View File

@ -42,7 +42,7 @@ public class DefaultWorkflowProcessorFactory implements IServerFactory<WorkflowP
List<WorkflowProcessor> list = new ArrayList<WorkflowProcessor>();
for (MWorkflowProcessor pModel : workflowModels)
{
WorkflowProcessor server = new WorkflowProcessor(pModel);
WorkflowProcessor server = create(ctx, pModel);
list.add(server);
}
return list.toArray(new WorkflowProcessor[0]);
@ -55,4 +55,10 @@ public class DefaultWorkflowProcessorFactory implements IServerFactory<WorkflowP
public Class<MWorkflowProcessor> getProcessorClass() {
return MWorkflowProcessor.class;
}
@Override
public WorkflowProcessor create(Properties ctx,
MWorkflowProcessor serverModel) {
return new WorkflowProcessor(serverModel);
}
}