怎么使用RelProxy提高Java开发效率

本篇内容介绍了“怎么使用RelProxy提高Java开发效率”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联建站成立以来不断整合自身及行业资源、不断突破观念以使企业策略得到完善和成熟,建立了一套“以技术为基点,以客户需求中心、市场为导向”的快速反应体系。对公司的主营项目,如中高端企业网站企划 / 设计、行业 / 企业门户设计推广、行业门户平台运营、APP应用开发手机网站制作、微信网站制作、软件开发、内蒙古服务器托管等实行标准化操作,让客户可以直观的预知到从创新互联建站可以获得的服务效果。

RelProxy 旨在通过下列两种方式提高开发效率:

可以在生产环境下修改用户代码,而不需要重新加载整个应用。

提高开发效率,避免花费过多的时间加载应用且对性能不会有影响。

两个目标都要求在你的应用中增加一些 RelProxy 代码,注册成一种典型的监听、回调模式。这是一种“侵入”的方式。

如果你是一名Java 框架或独立 Java 通用服务模块的开发者,可以将 RelProxy Java 嵌入到你的框架中,这样能透明地为框架的终端用户提供代码自动加载功能,只需要进行一些必要的配置,而无需调用 RelProxy API。

对使用 Java 版的 RelProxy,有两种 API 可供调用:

JProxy 及其相关类:主要是静态方法

Java 脚本 API:基于接口

第二种方式更适合将 RelProxy 嵌入到你的 Java 框架中,这种方式是基于接口的,在你的 API 中无需暴露公共 RelProxy  类,因为在框架中会执行启动程序。我将使用比较简单的 API:JProxyScriptEngineFactory.create()。

JProxyScriptEngine 的功能与 Jproxy 相同,也就是说具有相同的方法。只是这种情况下,只需要使用接口。

一个简单的例子是演示如何嵌入 RelProxy 的***方式。这个例子是 RelProxy 的示例仓库中包含的  RelProxyBuiltin(relproxy_builtin_ex  项目中)。它定义了两个监听器来实现注册用户端的代码,一个监听器显示选项(option),另一个执行选择的行为。

这个迷你框架和示例使用 NetBeans 和 Maven 开发完成。

有两个包:

com.innowhere.relproxy_builtin_ex :迷你框架。子包 com.innowhere.relproxy_builtin_ex.impl 只包含一个非公共的类。

com.innowhere.relproxy_builtin_ex_main :一个简单的使用示例。

迷你框架(公共类和接口):

RelProxyBuiltinRoot.java

1

2

3

4

5

6

7

8

9

10

package com.innowhere.relproxy_builtin_ex;

import com.innowhere.relproxy_builtin_ex.impl.RelProxyBuiltinImpl;

public class RelProxyBuiltinRoot

{

    private final static RelProxyBuiltinImpl SINGLETON = new RelProxyBuiltinImpl();

    public static RelProxyBuiltin get()

    {

        return SINGLETON;

    }

}

RelProxyBuiltin.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package com.innowhere.relproxy_builtin_ex;

import com.innowhere.relproxy.jproxy.JProxyScriptEngine;

import java.io.InputStream;

import java.io.PrintStream;

public interface RelProxyBuiltin

{

    public JProxyScriptEngine getJProxyScriptEngine();

    public void addOutputListener(OutputListener listener);

    public void removeOutputListener(OutputListener listener);

    public int getOutputListenerCount();

    public void addCommandListener(CommandListener listener);

    public void removeCommandListener(CommandListener listener);

    public int getCommandListenerCount();

    public void runLoop(InputStream in,PrintStream out);

}

OutputListener.java

1

2

3

4

5

6

package com.innowhere.relproxy_builtin_ex;

import java.io.PrintStream;

public interface OutputListener

{

    public void write(PrintStream out);

}

CommandListener.java

1

2

3

4

5

6

package com.innowhere.relproxy_builtin_ex;

import java.io.PrintStream;

public interface CommandListener

{

    public void execute(String command,String input,PrintStream out);

}

现在看一下实现细节,该类演示了怎样简单地内嵌 RelProxy:

RelProxyBuiltinImpl.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

package com.innowhere.relproxy_builtin_ex.impl;

import com.innowhere.relproxy.jproxy.JProxyScriptEngine;

import com.innowhere.relproxy.jproxy.JProxyScriptEngineFactory;

import com.innowhere.relproxy_builtin_ex.CommandListener;

import com.innowhere.relproxy_builtin_ex.OutputListener;

import com.innowhere.relproxy_builtin_ex.RelProxyBuiltin;

import java.io.InputStream;

import java.io.PrintStream;

import java.util.LinkedHashSet;

import java.util.Scanner;

public class RelProxyBuiltinImpl implements RelProxyBuiltin

{

    protected JProxyScriptEngine jProxyEngine = null;

    protected LinkedHashSet outListeners = new LinkedHashSet();

    protected LinkedHashSet  commandListeners = new LinkedHashSet();

    @Override

    public JProxyScriptEngine getJProxyScriptEngine()

    {

        if (jProxyEngine == null) jProxyEngine = (JProxyScriptEngine)JProxyScriptEngineFactory.create().getScriptEngine();

        return jProxyEngine;

    }

    public JProxyScriptEngine getJProxyScriptEngineIfConfigured()

    {

        if (jProxyEngine == null || !jProxyEngine.isEnabled())

            return null;

        return jProxyEngine;

    }

    @Override

    public void addOutputListener(OutputListener listener)

    {

        JProxyScriptEngine jProxy = getJProxyScriptEngineIfConfigured();

        if (jProxy != null)

        {

            listener = jProxy.create(listener,OutputListener.class);

        }

        outListeners.add(listener);

    }

    @Override

    public void removeOutputListener(OutputListener listener)

    {

        JProxyScriptEngine jProxy = getJProxyScriptEngineIfConfigured();

        if (jProxy != null)

        {

            listener = jProxy.create(listener,OutputListener.class);

        }

        outListeners.remove(listener);

    }

    @Override

    public int getOutputListenerCount()

    {

        return outListeners.size();

    }

    @Override

    public void addCommandListener(CommandListener listener)

    {

        JProxyScriptEngine jProxy = getJProxyScriptEngineIfConfigured();

        if (jProxy != null)

        {

            listener = jProxy.create(listener,CommandListener.class);

        }

        commandListeners.add(listener);

    }

    @Override

    public void removeCommandListener(CommandListener listener)

    {

        JProxyScriptEngine jProxy = getJProxyScriptEngineIfConfigured();

        if (jProxy != null)

        {

            listener = jProxy.create(listener,CommandListener.class);

        }

        commandListeners.remove(listener);

    }

    @Override

    public int getCommandListenerCount()

    {

        return commandListeners.size();

    }

    @Override

    public void runLoop(InputStream in,PrintStream out)

    {

        Scanner scanner = new Scanner(in);

        while(true)

        {

            out.print("Enter phrase:");

            String input = scanner.nextLine();

            out.println("Command list:");

            for(OutputListener listener : outListeners)

                listener.write(out);

            out.print("Enter command (or quit):");

            String command = scanner.nextLine();

            if ("quit".equals(command))

                break;

            for(CommandListener listener : commandListeners)

                listener.execute(command,input,out);

        }

    }

}

这三个方法足以解释怎样启动 RelProxy Java 引擎,怎样简单地使用指令监听器来注册热加载。

RelProxyBuiltinImpl.java (部分)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@Override

  public JProxyScriptEngine getJProxyScriptEngine()

  {

      if (jProxyEngine == null) jProxyEngine = (JProxyScriptEngine)JProxyScriptEngineFactory.create().getScriptEngine();

      return jProxyEngine;

  }

  public JProxyScriptEngine getJProxyScriptEngineIfConfigured()

  {

      if (jProxyEngine == null || !jProxyEngine.isEnabled())

          return null;

      return jProxyEngine;

  }

  @Override

  public void addOutputListener(OutputListener listener)

  {

      JProxyScriptEngine jProxy = getJProxyScriptEngineIfConfigured();

      if (jProxy != null)

      {

          listener = jProxy.create(listener,OutputListener.class);

      }

      outListeners.add(listener);

  }

公共方法 RelProxyBuiltin.getJProxyScriptEngine() 必须在启动时执行,用于配置 RelProxy。如果没有配置,RelProxy 就不起作用。

请记住,通过 create(…) 创建的代理对象需要能正确的执行 hashCode() 方法和 equals(Object) 方法,监听器集合、监听记录依赖这两个方法来区别监听器对象。

这是基于控制台的示例代码(名称与 JUnit 类似,但确实不是 JUnit 的测试示例):

Main.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

package com.innowhere.relproxy_builtin_ex_main;

import com.innowhere.relproxy.RelProxyOnReloadListener;

import com.innowhere.relproxy.jproxy.JProxy;

import com.innowhere.relproxy.jproxy.JProxyCompilerListener;

import com.innowhere.relproxy.jproxy.JProxyConfig;

import com.innowhere.relproxy.jproxy.JProxyDiagnosticsListener;

import com.innowhere.relproxy.jproxy.JProxyInputSourceFileExcludedListener;

import com.innowhere.relproxy.jproxy.JProxyScriptEngine;

import com.innowhere.relproxy_builtin_ex.CommandListener;

import com.innowhere.relproxy_builtin_ex.RelProxyBuiltin;

import com.innowhere.relproxy_builtin_ex.RelProxyBuiltinRoot;

import java.io.File;

import java.lang.reflect.Method;

import java.net.URL;

import java.util.Arrays;

import java.util.List;

import javax.tools.Diagnostic;

import javax.tools.DiagnosticCollector;

import javax.tools.JavaFileObject;

public class Main

{

    public static void main(String[] args) throws Exception

    {

        new Main();

    }

    public Main()

    {

        // Note: NetBeans Console window works bad (no input) with Maven Test tasks http://stackoverflow.com/questions/3035351/broken-console-in-maven-project-using-netbeans

        // this is why is not a really JUnit test.

        setUp();

        try

        {

            mainTest();

        }

        finally

        {

            tearDown();

        }

        System.exit(0);

    }

    public void setUp()

    {

        URL res = this.getClass().getResource("/"); // .../target/classes/

        // Use example of RelProxy in development time:

        String inputPath = res.getFile() + "/../../src/main/java/";

        if (new File(inputPath).exists())

        {

            System.out.println("RelProxy to be enabled, development mode detected");

        }

        else

        {

            System.out.println("RelProxy disabled, production mode detected");

            return;

        }

        JProxyInputSourceFileExcludedListener excludedListener = new JProxyInputSourceFileExcludedListener()

        {

            @Override

            public boolean isExcluded(File file, File rootFolderOfSources)

            {

                String absPath = file.getAbsolutePath();

                if (file.isDirectory())

                {

                    return absPath.endsWith(File.separatorChar + "relproxy_builtin_ex");

                }

                else

                {

                    return absPath.endsWith(File.separatorChar + Main.class.getSimpleName() + ".java");

                }

            }

        };

        String classFolder = null; // Optional

        Iterable compilationOptions = Arrays.asList(new String[]{"-source","1.6","-target","1.6"});

        long scanPeriod = 1000;

        RelProxyOnReloadListener proxyListener = new RelProxyOnReloadListener() {

            @Override

            public void onReload(Object objOld, Object objNew, Object proxy, Method method, Object[] args) {

                System.out.println("Reloaded " + objNew + " Calling method: " + method);

            }

        };

        JProxyCompilerListener compilerListener = new JProxyCompilerListener(){

            @Override

            public void beforeCompile(File file)

            {

                System.out.println("Before compile: " + file);

            }

            @Override

            public void afterCompile(File file)

            {

                System.out.println("After compile: " + file);

            }

        };

        JProxyDiagnosticsListener diagnosticsListener = new JProxyDiagnosticsListener()

        {

            @Override

            public void onDiagnostics(DiagnosticCollector diagnostics)

            {

                Listextends JavaFileObject>> diagList = diagnostics.getDiagnostics();

                int i = 1;

                for (Diagnostic diagnostic : diagList)

                {

                   System.err.println("Diagnostic " + i);

                   System.err.println("  code: " + diagnostic.getCode());

                   System.err.println("  kind: " + diagnostic.getKind());

                   System.err.println("  line number: " + diagnostic.getLineNumber());

                   System.err.println("  column number: " + diagnostic.getColumnNumber());

                   System.err.println("  start position: " + diagnostic.getStartPosition());

                   System.err.println("  position: " + diagnostic.getPosition());

                   System.err.println("  end position: " + diagnostic.getEndPosition());

                   System.err.println("  source: " + diagnostic.getSource());

                   System.err.println("  message: " + diagnostic.getMessage(null));

                   i++;

                }

            }

        };

        RelProxyBuiltin rpbRoot = RelProxyBuiltinRoot.get();

        JProxyScriptEngine engine = rpbRoot.getJProxyScriptEngine();

        JProxyConfig jpConfig = JProxy.createJProxyConfig();

        jpConfig.setEnabled(true)

                .setRelProxyOnReloadListener(proxyListener)

                .setInputPath(inputPath)

                .setJProxyInputSourceFileExcludedListener(excludedListener)

                .setScanPeriod(scanPeriod)

                .setClassFolder(classFolder)

                .setCompilationOptions(compilationOptions)

                .setJProxyCompilerListener(compilerListener)

                .setJProxyDiagnosticsListener(diagnosticsListener);

        engine.init(jpConfig);

        System.out.println("RelProxy running");

    }

    public void tearDown()

    {

        RelProxyBuiltin rpbRoot = RelProxyBuiltinRoot.get();

        JProxyScriptEngine engine = rpbRoot.getJProxyScriptEngine();

        engine.stop();

        System.out.println("RelProxy stopped");

    }

    public void mainTest()

    {

        RelProxyBuiltin rpbRoot = RelProxyBuiltinRoot.get();

        TestListener listener = new TestListener();

        rpbRoot.addOutputListener(listener);

        assertTrue(rpbRoot.getOutputListenerCount() == 1);

        rpbRoot.removeOutputListener(listener);

        assertTrue(rpbRoot.getOutputListenerCount() == 0);

        rpbRoot.addOutputListener(listener);

        CommandListener commandListener = listener.getCommandListener();

        rpbRoot.addCommandListener(commandListener);

        assertTrue(rpbRoot.getCommandListenerCount() == 1);

        rpbRoot.removeCommandListener(commandListener);

        assertTrue(rpbRoot.getCommandListenerCount() == 0);

        rpbRoot.addCommandListener(commandListener);

        rpbRoot.runLoop(System.in,System.out);

    }

    private static void assertTrue(boolean res)

    {

        if (!res) throw new RuntimeException("Unexpected Error");

    }

}

看一下这段代码:

Main.java (部分)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

URL res = this.getClass().getResource("/"); // .../target/classes/

       // Use example of RelProxy in development time:

       String inputPath = res.getFile() + "/../../src/main/java/";

       if (new File(inputPath).exists())

       {

           System.out.println("RelProxy to be enabled, development mode detected");

       }

       else

       {

           System.out.println("RelProxy disabled, production mode detected");

           return;

       }

       JProxyInputSourceFileExcludedListener excludedListener = new JProxyInputSourceFileExcludedListener()

       {

           @Override

           public boolean isExcluded(File file, File rootFolderOfSources)

           {

               String absPath = file.getAbsolutePath();

               if (file.isDirectory())

               {


文章题目:怎么使用RelProxy提高Java开发效率
文章链接:http://myzitong.com/article/ipcegi.html