[英]how to read value of config.rpc.port from build.gradle to java class in corda
I have created one constants.java file as below, 我创建了一个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;
}
but in my above class I have hardcoded the value, In my application I want this values should be generic.For that I want to read these values from build.gradle. 但是在上面的类中,我已经对该值进行了硬编码,在我的应用程序中,我希望该值应该是通用的。为此,我想从build.gradle中读取这些值。
This is my build.gradle files, 这是我的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"
}
In kotlin we have option to read values from build.gradle as, 在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 {
}
but the same is not allowing in java. 但同样不允许在Java中使用。 Kindly let me now how to do the same in java. 现在让我在Java中执行相同的操作。
In Java, you would define a Constants.java
file as follows: 在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";
}
And a NodeRPCConnection.java
file as follows: 和一个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.