📄 acctservice.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.acct;
import java.util.*;
import java.sql.*;
import javax.management.*;
import org.jboss.system.*;
import org.compiere.util.DB;
/**
* Account Service MBean
*
* @author Jorg Janke
* @version $Id: AcctService.java,v 1.2 2002/11/18 06:11:57 jjanke Exp $
*/
public class AcctService extends ServiceMBeanSupport
implements AcctServiceMBean
{
/**
* Acct Service
*/
public AcctService()
{
super();
} // AcctService
/**
* Get Name
* @return Name
*/
public String getName()
{
return NAME;
} // getName
/** Worker Thread */
private AcctServer[] m_worker = null;
/** SleepMinutes */
private int m_sleepMinutes = 5;
/** Number of Invocations */
private volatile int m_count = 0;
/** Number of workers (1) */
private int m_noWorkers = 1;
/** Max Sleep time */
private int m_maxSleepMinutes = 10;
/** Batch Size per Query (20) */
private int m_batchSize = 20;
/** List of Clients */
private ArrayList m_clients = new ArrayList();
/** Total Work Queue */
private AcctServerWork[] m_totalQueue = null;
/** DataSource Name */
private String m_dataSourceName;
/*************************************************************************/
/**
* Create Service
* @throws Exception
*/
protected void createService() throws Exception
{
} // createService
/**
* Start Service
* @throws Exception
*/
protected void startService() throws Exception
{
if (m_worker == null || m_worker.length != m_noWorkers)
m_worker = new AcctServer [m_noWorkers];
if (m_clients.size() == 0)
addAllClients();
for (int i = 0; i < m_worker.length; i++)
{
m_worker[i] = new AcctServer (i, m_sleepMinutes, m_maxSleepMinutes,
m_batchSize, createWork(i), false);
m_worker[i].setPriority(Thread.MIN_PRIORITY);
m_worker[i].setDaemon(true);
m_worker[i].start();
}
} // startService
/**
* Stop Service
* @throws Exception
*/
protected void stopService() throws Exception
{
if (m_worker == null)
return;
try
{
for (int i = 0; i < m_worker.length; i++)
{
if (m_worker[i] != null && m_worker[i].isAlive())
{
m_worker[i].interrupt();
m_worker[i].setPriority(Thread.NORM_PRIORITY);
}
}
log.info("stopService - interrupted waiting ...");
for (int i = 0; i < m_worker.length; i++)
{
m_worker[i].join (10000); // 10 sec max
m_worker[i] = null;
}
m_worker = null;
}
catch (Exception e)
{
log.error("stopService", e);
}
} // stopService
/**
* Destroy Service
* @throws Exception
*/
protected void destroyService() throws Exception
{
m_worker = null;
} // destroyService
/**
* Are all workers Active ?
* @return true if All Alive
*/
public synchronized boolean isAllAlive()
{
if (m_worker == null)
return false;
for (int i = 0; i < m_worker.length; i++)
{
if (m_worker[i] == null && !m_worker[i].isAlive())
return false;
}
return true;
} // isAllActive
/*************************************************************************/
/**
* Set Sleep Minutes
* @param sleepMinutes sleep time in minutes
*/
public void setSleepMinutes (int sleepMinutes)
{
m_sleepMinutes = sleepMinutes;
} // setSleepMinutes
/**
* Get Sleep Minutes
* @return sleep minutes
*/
public int getSleepMinutes ()
{
return m_sleepMinutes;
} // getSleepMinutes
/**
* Get Statistics
* @return statistical info
*/
public String getStatistics()
{
StringBuffer sb = new StringBuffer();
// Processor
sb.append("WorkerCount=").append(m_noWorkers);
sb.append(",AllAlive=").append(isAllAlive());
if (m_worker == null)
return sb.toString();
// Worker
for (int i = 0; i < m_worker.length; i++)
{
sb.append(";Worker_").append(i).append("=");
if (m_worker[i] == null)
sb.append("null");
else if (!m_worker[i].isAlive())
sb.append("NotAlive");
else if (m_worker[i].isInterrupted())
sb.append("Interrupted");
else
sb.append("Alive");
}
//
return sb.toString();
} // getStatistics
/**
* Get DataSource Name
* @return DataSource Name
*/
public String getDataSourceName()
{
return m_dataSourceName;
} // getDataSourceName
/**
* Set DataSource Name
* @param dataSourceName DataSource Name
*/
public void setDataSourceName (String dataSourceName)
{
m_dataSourceName = dataSourceName;
} // setDataSourceName
/*************************************************************************/
/**
* Set Number of Workers.
* (default: one per client)
* Can only be set before first start
* @param noWorkers workers
*/
public void setWorkerCount (int noWorkers)
{
m_noWorkers = noWorkers;
} // setNoWorkers
/**
* Get Number of Workers
* @return Number of Workers
*/
public int getWorkerCount()
{
return m_noWorkers;
} // getNoWorkers
/**
* Set Max Number of minutes to sleep after each round.
* (default: 10)
* @param maxSleepMinutes minutes
*/
public void setMaxSleepMinutes (int maxSleepMinutes)
{
m_maxSleepMinutes = maxSleepMinutes;
} // setMaxSleepMinutes
/**
* Get Max Sleep Minutes
* @return Sleep Minutes
*/
public int getMaxSleepMinutes()
{
return m_maxSleepMinutes;
} // getSleepMinutes
/**
* Set Batch Size (0 = all documents).
* (default: 20)
* @param batchSize batch size
*/
public void setBatchSize (int batchSize)
{
m_batchSize = batchSize;
} // setBatchSize
/**
* Get Batch Size
* @return Batch Size
*/
public int getBatchSize()
{
return m_batchSize;
} // getBatchSize
/**
* Add Client to List of Clients
* @param AD_Client_ID client
*/
public void addClientID (int AD_Client_ID)
{
log.info("addClient " + AD_Client_ID);
m_clients.add (new Integer(AD_Client_ID));
} // addClient
/**
* Add All Clients
*/
public void addAllClients()
{
log.info("addAllClients");
String sql = "SELECT AD_Client_ID FROM AD_Client WHERE IsActive='Y' AND AD_Client_ID <> 0";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
addClientID (rs.getInt(1));
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.error("addAllClients", e);
}
} // addAllClients
/**
* Remove Client from List of Clients
* @param AD_Client_ID client
*/
public void removeClientID (int AD_Client_ID)
{
Integer c = new Integer(AD_Client_ID);
if (m_clients.contains(c))
{
if (m_clients.remove(c))
log.info("removeClient " + AD_Client_ID);
else
log.warn("removeClient " + AD_Client_ID + " - not removed");
}
else
log.warn("removeClient " + AD_Client_ID + " - does not exist");
} // removeClient
/**
* Get Client Number
* @return Number of Clients
*/
public int getClientCount()
{
return m_clients.size();
} // getNoClients
/**
* Get Client List as String
* @return AD_Client_ID list
*/
public String getClientList()
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < m_clients.size(); i++)
{
if (i != 0)
sb.append(", ");
sb.append(m_clients.get(i));
}
return sb.toString();
} // getClientList
/**
* Create Work.
* Create [client,table] pairs and distribute them among workers
* @param workerNo worker
* @return AcctServerWork Array
*/
private synchronized AcctServerWork[] createWork (int workerNo)
{
// Create TotalWork
if (m_totalQueue == null)
{
m_totalQueue = new AcctServerWork [ m_clients.size() * Doc.documents.length ];
int index = 0;
for (int c = 0; c < m_clients.size(); c++)
{
int AD_Client_ID = ((Integer)m_clients.get(c)).intValue();
for (int t = 0; t < Doc.documents.length; t++)
m_totalQueue[index++] = new AcctServerWork (AD_Client_ID, Doc.documents[t]);
}
}
int no = m_totalQueue.length / m_worker.length;
int start = no * workerNo;
int end = (workerNo+1) * no;
if (workerNo == m_worker.length-1) // last worker gets rest
end = m_totalQueue.length;
log.debug ("createWork for Worker=" + workerNo
+ "TotalQueue=" + m_totalQueue.length + ", WorkerQueue=" + (end-start)
+ ", Index: " + start + " - " + end);
AcctServerWork[] retValue = new AcctServerWork[end-start];
int index = 0;
for (int i = start; i < end; i++)
retValue[index++] = m_totalQueue[i];
return retValue;
} // createWork
/**
* Run Now
*/
public void runNow()
{
AcctServer wk = new AcctServer (99, m_sleepMinutes, m_maxSleepMinutes,
m_batchSize, m_totalQueue, true);
wk.setPriority(Thread.MIN_PRIORITY);
wk.setDaemon(true);
wk.start();
} // runNow
} // AcctService
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -