1. 程式人生 > >通用單例模式客戶端初始化

通用單例模式客戶端初始化

HR span except util rop 16px exce div 參數

 1 package util;
 2 import org.web3j.protocol.geth.Geth;
 3 import org.web3j.protocol.http.HttpService;
 4 
 5 public class GethClientUtil {
 6 
 7     private volatile static Geth geth;
 8 
 9     public static Geth getClient() {
10         if (geth == null) {
11             synchronized (GethClientUtil.class
) { 12 if (geth == null) { 13 try { 14 geth = Geth.build(new HttpService(PropUtil.getProps().getProperty("RPC_ADDR"))); 15 } catch (Exception e) { 16 e.printStackTrace(); 17 }
18 } 19 } 20 } 21 return geth; 22 } 23 24 }

配置文件工具類

 1 package util;
 2 
 3 import java.io.InputStream;
 4 import java.util.Properties;
 5 
 6 public class PropUtil {
 7 
 8     private static Properties props = null;
 9     
10     static {
11         readProperties("ether.properties");
12 } 13 14 private static void readProperties(String fileName) { 15 try { 16 props = new Properties(); 17 InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); 18 props.load(inputStream); 19 inputStream.close(); 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 } 24 25 public static Properties getProps() { 26 return props; 27 } 28 29 }

配置文件

1 #調用RPC地址
2 RPC_ADDR=http://127.0.0.1:8545
3 #礦工費參數
4 GAS_PRICE=22000000000
5 GAS_LIMIT=4300000
6 #私鑰文件存儲路徑
7 KEYSTORE_PATH=E:/Project/TestGeth/keystore

通用單例模式客戶端初始化