Generic singleton pattern client initialization

 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 }

 Configuration file tool class

 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 }

configuration file

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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325297627&siteId=291194637