[英]how to read value of config.rpc.port from build.gradle to java class in corda
我創建了一個constants.java文件,如下所示,
public class Constants {
public static final String CORDA_USER_NAME = "user1";
public static final String CORDA_USER_PASSWORD = "test";
public static final String CORDA_NODE_HOST = "localhost";
public static final int CORDA_RPC_PORT = 10009;
}
但是在上面的類中,我已經對該值進行了硬編碼,在我的應用程序中,我希望該值應該是通用的。為此,我想從build.gradle中讀取這些值。
這是我的build.gradle文件,
task runPartyAServer(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'net.corda.Server'
environment "server.port", "10022"
environment "config.rpc.username", "user1"
environment "config.rpc.password", "test"
environment "config.rpc.host", "localhost"
environment "config.rpc.port", "10006"
}
task runPartyBServer(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'net.corda.Server'
environment "server.port", "10033"
environment "config.rpc.username", "user1"
environment "config.rpc.password", "test"
environment "config.rpc.host", "localhost"
environment "config.rpc.port", "10009"
}
在Kotlin中,我們可以選擇從build.gradle中讀取值,
private const val CORDA_USER_NAME = "config.rpc.username"
private const val CORDA_USER_PASSWORD = "config.rpc.password"
private const val CORDA_NODE_HOST = "config.rpc.host"
private const val CORDA_RPC_PORT = "config.rpc.port"
@Component
open class NodeRPCConnection(
@Value("\${$CORDA_NODE_HOST}") private val host: String,
@Value("\${$CORDA_USER_NAME}") private val username: String,
@Value("\${$CORDA_USER_PASSWORD}") private val password: String,
@Value("\${$CORDA_RPC_PORT}") private val rpcPort: Int): AutoCloseable {
}
但同樣不允許在Java中使用。 現在讓我在Java中執行相同的操作。
在Java中,您將定義Constants.java
文件,如下所示:
package net.corda.server;
public final class Constants {
public static final String CORDA_USER_NAME = "config.rpc.username";
public static final String CORDA_USER_PASSWORD = "config.rpc.password";
public static final String CORDA_NODE_HOST = "config.rpc.host";
public static final String CORDA_RPC_PORT = "config.rpc.port";
}
和一個NodeRPCConnection.java
文件如下:
package net.corda.server;
import net.corda.client.rpc.CordaRPCClient;
import net.corda.client.rpc.CordaRPCConnection;
import net.corda.core.messaging.CordaRPCOps;
import net.corda.core.utilities.NetworkHostAndPort;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import static net.corda.server.Constants.*;
/**
* Wraps a node RPC proxy.
* <p>
* The RPC proxy is configured based on the properties in `application.properties`.
*
* @param host The host of the node we are connecting to.
* @param rpcPort The RPC port of the node we are connecting to.
* @param username The username for logging into the RPC client.
* @param password The password for logging into the RPC client.
* @property proxy The RPC proxy.
*/
@Component
public class NodeRPCConnection implements AutoCloseable {
@Value("${" + CORDA_NODE_HOST + "}")
private String host;
@Value("${" + CORDA_USER_NAME + "}")
private String username;
@Value("${" + CORDA_USER_PASSWORD + "}")
private String password;
@Value("${" + CORDA_RPC_PORT + "}")
private int rpcPort;
private CordaRPCConnection rpcConnection;
public CordaRPCOps proxy;
@PostConstruct
public void initialiseNodeRPCConnection() {
NetworkHostAndPort rpcAddress = new NetworkHostAndPort(host, rpcPort);
CordaRPCClient rpcClient = new CordaRPCClient(rpcAddress);
CordaRPCConnection rpcConnection = rpcClient.start(username, password);
proxy = rpcConnection.getProxy();
}
@PreDestroy
public void close() {
rpcConnection.notifyServerAndClose();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.