91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用RelProxy提高Java開發效率

發布時間:2021-08-05 18:31:30 來源:億速云 閱讀:158 作者:chen 欄目:編程語言

本篇內容介紹了“怎么使用RelProxy提高Java開發效率”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

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<OutputListener> outListeners = new LinkedHashSet<OutputListener>();

    protected LinkedHashSet<CommandListener>  commandListeners = new LinkedHashSet<CommandListener>();

    @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(&hellip;) 創建的代理對象需要能正確的執行 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<String> 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<JavaFileObject> diagnostics)

            {

                List<Diagnostic<? extends 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())

               {

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

               }

               else

               {

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

               }

           }

       };

我們獲取并注冊應用源代碼的根目錄,該代碼“可能”會被重新加載。

我們需要排除框架代碼,因為這顯然不是用戶的代碼(不需要重新加載)。此外,還需要排除 Main.java  文件,該文件包含了測試代碼,也不需要重新加載,只有 TestListener.java 類(與 Main.java  在同一文件夾下)需要(必需)重新加載。

*** TestListener.java 類包含兩個監聽器,CommandListener 的實現采用匿名內部類的方式,主要目的是為了演示。

TestListener.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

package com.innowhere.relproxy_builtin_ex_main;

import com.innowhere.relproxy_builtin_ex.CommandListener;

import com.innowhere.relproxy_builtin_ex.OutputListener;

import java.io.PrintStream;

public class TestListener implements OutputListener

{

    @Override

    public void write(PrintStream out)

    {

        out.println("uppercase");

        out.println("lowercase");

    }

    public CommandListener getCommandListener()

    {

        return new CommandListener()

        {

            @Override

            public void execute(String command,String text,PrintStream out)

            {

                if ("uppercase".equals(command))

                    out.println(text.toUpperCase());

                else if ("lowercase".equals(command))

                    out.println(text.toLowerCase());

                else

                    out.println("Unknown command:" + command);

            }

        };

    }

}

先預定義可選項,然后執行 Main 類。為了校驗 RelProxy 是否起作用,可以在不停止程序的運行的基礎上增加一個新的可選項“same”。

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

@Override

    public void write(PrintStream out)

    {

        out.println("uppercase");

        out.println("lowercase");

        out.println("same");  // NEW

    }

    public CommandListener getCommandListener()

    {

        return new CommandListener()

        {

            @Override

            public void execute(String command,String text,PrintStream out)

            {

                if ("uppercase".equals(command))

                    out.println(text.toUpperCase());

                else if ("lowercase".equals(command))

                    out.println(text.toLowerCase());

                else if ("same".equals(command)) // NEW

                    out.println(text); // NEW

                else

                    out.println("Unknown command:" + command);

            }

        };

    }

}

“怎么使用RelProxy提高Java開發效率”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

赤城县| 乌兰察布市| 金沙县| 昭苏县| 叙永县| 揭阳市| 霍林郭勒市| 珠海市| 河津市| 南康市| 尼木县| 阿巴嘎旗| 麻栗坡县| 祁门县| 沐川县| 山丹县| 玉田县| 黄浦区| 响水县| 寿光市| 西宁市| 景洪市| 内黄县| 瑞金市| 祁阳县| 顺昌县| 香港| 新营市| 隆尧县| 天柱县| 巴彦淖尔市| 卢氏县| 阿拉尔市| 达州市| 灵台县| 灌南县| 炉霍县| 洪洞县| 当涂县| 特克斯县| 宁津县|