用 Proxy 简化加锁代码,用 ReadWriteLock 以提高并行性

阅读更多
java.lang.reflect.Proxy 简化加锁代码,用 ReadWriteLock 以提高并行性
 
有一个 CoreManager 接口,用来代表一个服务,每个方法对应服务的一个子功能。
 
方法 startup() 要求服务在 stoped 状态。
方法 shutdown() 要求服务在 started 状态。
方法 isStarted() 要求服务在 stoped/started 状态。
其它方法,要求服务在 started 状态。
 
 

/**
  * All mehtod will throw {@link ManageException} then Target server stoped.
  *
  */
public interface CoreManager {
 
      // ------- Target Server Control ---------------------------
      /**
        * Shutdown Target Server Core.
        *  
        * @throws ManageException startup error
        */
      public void startup() throws ManageException;
 
      /**
        * Shutdown Target Server Core.
        *  
        * @throws ManageException shutdown error
        */
      public void shutdown() throws ManageException;
     
      /**
        * return ture if Target Server is started
        * @return the result
        *  
        */
      public boolean isStarted();
     
      /**
        * Reconfigure the log setting
        *
        * @param logConfig
        * @throws ManageException
        */
      public void configureLog(LogConfig logConfig) throws ManageException;
 
      // ------- Component Manage-------------------------------
      /**
        * Add component.
        *
        * @param componentInfo
        * @throws ManageException add fail.
        */
      public void addComponent(AbstractComponentBean componentInfo)
                  throws ManageException;
 
      /**
        * Return all component names.
        *
        * @return not null
        * @throws ManageException
        */
      public String[] getAllComponentNames() throws ManageException;
 
      /**
        * Return the component setting.
        *
        * @param componentName
        * @return result not null
        * @throws ManageException component not found
        */
      public AbstractComponentBean getComponentInfo(String componentName)
                  throws ManageException;
 
      /**
        * Remove component.
        *
        * @param componentName
        * @throws ManageException component not found
        */
      public void removeComponent(String componentName) throws ManageException;
 
      /**
        * Apply new routing graph
        *
        * @deprecated use {@link #setNewRouting(ComponentConnector[])} instead
        * @param routingBeans
        * @return ture only if all connector is seted
        * @throws ManageException
        */
      @Deprecated
      public boolean setNewRouting(ConnectorBean[] routingBeans) throws ManageException;
 
      /**
        * Apply new routing graph
        *
        * @param routingBeans
        * @return ture only if all connector is seted
        * @throws ManageException
        */
      public boolean setNewRouting(ComponentConnector[] routingBeans) throws ManageException;
 
      /**
        * Change component setting
        *
        * @param componentBean
        * @throws ManageException component not found
        */
      public void changeComponentSetting(AbstractComponentBean componentBean)
                  throws ManageException;
 
      /**
        * Return true if component exsit.
        *
        * @param componentName
        * @return the result
        * @throws ManageException
        */
      public boolean isComponentExist(String componentName) throws ManageException;
 
      /**
        * Get the device component status.
        * If the component in componentNames not exist, UNKNOWN will return.
        *
        * @param componentNames
        * @return result
        * @throws ManageException
        */
      public ConnectionStatus[] getDeviceComponentStatus(String[] componentNames) throws ManageException;
 
      /**
        * Set the enabled property of component.
        *
        * @param componentName
        * @param enabled
        * @throws ManageException component not found.
        */
      public void setComponentEnabledProperty(String componentName,
                  boolean enabled) throws ManageException;
 
      // ------- Reader Control ----------------------------------
      public void setAutoModeOn(String deviceName) throws ManageException;
 
      public void setAutoModeOff(String deviceName) throws ManageException;
 
      public String getVersionNumber(String readerName) throws ManageException;
 
      public long getNumberOfReads(String readerName) throws ManageException;
 
      public long [] getNumberOfReads(String[] readerNames) throws ManageException;
 
      public Collection readTags(String readerName)
                  throws ManageException;
 
      public Collection readTags(String readerName, int antennaSequenceNo)
                  throws ManageException;
 
      public Collection readTags(String readerName,
                  int [] antennaSequenceNo) throws ManageException;
 
      public void writeTag(String deviceName, String writeFlag, String epc,
                  String userData, String killCode, int antennaSequenceNo)
                  throws ManageException;
 
      public void killTag(String deviceName, String uri, String killCode,
                  int antennaSequence) throws ManageException;
 
      public void setReadPower(String deviceName, int powerPercent)
                  throws ManageException;
 
      /**
        * Send a command(Event) to a component
        *
        * @param targetCompName
        * @param event
        * @return may null
        * @throws ManageException Component not found/stoped.
        */
      public Object sendEvent2Component(String targetCompName, Object event)
                  throws ManageException;
 
      /**
        * Send a command(Event) to a component
        *
        * @param targetCompName
        * @param srcCompInfo
        * @param event
        * @return may null
        * @throws ManageException Component not found/stoped.
        */
      public Object sendComponentEvent2Component(String targetCompName,
                  ConnectorEndpoint srcCompInfo, Object event)
                  throws ManageException;
     
     
      public List getAllConnectors() throws ManageException;
 
      public void addConnector(ComponentConnector connector) throws ManageException;
 
      public void removeConnector(ComponentConnector connector) throws ManageException;
 
}

 
 
有这样一个最核心的实现:
 

public class ManagerImpl implements CoreManager {
 
      private final TargetServerCore TargetServer ;
 
      private static final Log log = LogFactory.getLog( ManagerImpl. class );
 
      public ManagerImpl(TargetServerCore TargetServer) {
            this . TargetServer = TargetServer;

你可能感兴趣的:(用 Proxy 简化加锁代码,用 ReadWriteLock 以提高并行性)