Using a proxy template to write a server is a good idea?

For a school project, I need to write a simple Java server that constantly listens on the incoming directory and moves files from this directory to another location. The server needs to log information and error messages, so I decided to use a proxy template for this. So I created the following ServerInterface:

public interface ServerInterface extends Runnable {

    public void initialize(String repPath, ExecutorInterface executor, File propertiesFile) throws ServerInitException;

    public void run();

    public void terminate();

    public void updateHTML();

    public File[] scanIncomingDir();

    public List<DatasetAttributes> moveIncomingFilesIfComplete(File[] incomingFiles);

}

Then I created an implementation Serverrepresenting the real object and a class ProxyServerrepresenting the proxy. It also Serverhas a factory method that creates an object ProxyServer, but returns it as ServerInterface.

The method runin the proxy object is as follows:

@Override
    public void run(){

        log(LogLevels.INFO, "server is running ...");

        while( !stopped ){

            try {

                File[] incomingContent = scanIncomingDir();
                moveIncomingFilesIfComplete(incomingContent);
                updateHTML();
                pause();

            } catch (Exception e) {
                logger.logException(e, new Timestamp(timestampProvider.getTimestamp()));
                pause();
            }

        }

        log(LogLevels.INFO, "server stopped");
    }

, try, -, . . , run -, run , , ( , terminate).

, : ? -?

, , "" "" -behaviour... "" while, -, ? , :

  • run , - , while. - , , - .

  • , Proxy-Server Runnable, run terminate , .

? , ?

+4
4

. .

, , , , Aspect Oriented programming.

  • - , .

., - . ?

  • , A B, ( , ), OO ? .

-

- ​​ . :

  • , .
  • "" .

"" AOP

  • - Java DynamicProxy, . , , Spring Framework. .
  • - , ​​ asm, cglib, Javassist - .
  • - - ( -) .
  • - java- ( JVM).

AspectJ.

:

, (AOP), , , . , Spring Framework , , , , , , AOP.

. , , , ., , , , .

+2

:

, , , , , .

Observable , , , WatchService. , . .

, Observer Java JDK, java.util.Observable java.util.Observer.

0

- . - .

Before delegation, the proxy first registers the launch. After delegation, the proxy registers a shutdown:

// Snapshot from what should look like the run method implementation
// in your proxy.

public ServerInterfaceProxy(ServerInterface target){
    this.proxiedTarget = target;
}

public void run(){
    log(LogLevels.INFO, "server is running ...");
    this.proxiedTarget.run();
    log(LogLevels.INFO, "server is running ...");
}

This implementation can also be understood as an implementation of the Decorator template. IMHO, I believe that to some extent (when it comes to implementation), Proxy and Decorator are equivalent: like target interception / capture behavior.

0
source

Take a look at the Java 7 WatchService class.

Using proxy behavior for this is almost certainly crowded.

-1
source

All Articles