我在Stellar上提到了文档。
然后我开始在Java send payment之后运行receiving payment和creating an account代码。
发送付款代码正在运行,但收到付款代码已被终止。我在下面提到过代码:
public class receivePayment {
public static void main(String args[]) {
Server server = new Server("https://horizon-testnet.stellar.org");
KeyPair account = KeyPair.fromAccountId("GC2BKLYOOYPDEFJKLKY6FNNRQMGFLVHJKQRGNSSRRGSMPGF32LHCQVGF");
// Create an API call to query payments involving the account.
PaymentsRequestBuilder paymentsRequest = server.payments().forAccount(account);
// If some payments have already been handled, start the results from
// the
// last seen payment. (See below in `handlePayment` where it gets
// saved.)
/*
* String lastToken = loadLastPagingToken(); if (lastToken != null) {
* paymentsRequest.cursor(lastToken); }
*/
// `stream` will send each recorded payment, one by one, then keep the
// connection open and continue to send you new payments as they occur.
paymentsRequest.stream(new EventListener<OperationResponse>() {
@Override
public void onEvent(OperationResponse payment) {
// Record the paging token so we can start from here next time.
// savePagingToken(payment.getPagingToken());
// The payments stream includes both sent and received payments.
// We only
// want to process received payments here.
if (payment instanceof PaymentOperationResponse) {
if (((PaymentOperationResponse) payment).getTo().equals(account)) {
return;
}
String amount = ((PaymentOperationResponse) payment).getAmount();
Asset asset = ((PaymentOperationResponse) payment).getAsset();
String assetName;
if (asset.equals(new AssetTypeNative())) {
assetName = "lumens";
} else {
StringBuilder assetNameBuilder = new StringBuilder();
assetNameBuilder.append(((AssetTypeCreditAlphaNum) asset).getCode());
assetNameBuilder.append(":");
assetNameBuilder.append(((AssetTypeCreditAlphaNum) asset).getIssuer().getAccountId());
assetName = assetNameBuilder.toString();
}
StringBuilder output = new StringBuilder();
output.append(amount);
output.append(" ");
output.append(assetName);
output.append(" from ");
output.append(((PaymentOperationResponse) payment).getFrom().getAccountId());
System.out.println(output.toString());
}
}
});
}
}
我不明白为什么会被终止。如果我从我的帐户URL检查余额,但向我显示发送 - 接收结果,但它没有在Eclipse中显示结果。
我还提到了下面的参考链接,并按照答案但仍然没有用。
任何人都可以告诉我如何运行此代码,该代码不断收到付款并在控制台上维护日志。 ?
答案 0 :(得分:2)
问题是这是一个流服务,所以如果你只是在main方法中运行该服务,那么当它在main方法中运行时它将明显终止,并且范围将在外面,并且EventListener将无法执行。正如您所说的使用eclips,您可以做的一件事是运行try debug并在此Server server = new Server("https://horizon-testnet.stellar.org");
行插入一个调试点,然后按F6逐行。调试时一旦到达程序的最后一行然后等待,不要运行。您将在控制台中看到数据。这样你就会明白程序是如何工作的。
如果要快速运行它,请使用我在现有代码中添加的代码。我添加了两个选项。你可以使用任何一个。这将显示输出。
public class TestStellar2 {
public static void main(String args[]) {
Server server = new Server("https://horizon-testnet.stellar.org");
KeyPair account = KeyPair.fromAccountId("GC2BKLYOOYPDEFJKLKY6FNNRQMGFLVHJKQRGNSSRRGSMPGF32LHCQVGF");
PaymentsRequestBuilder paymentsRequest = server.payments().forAccount(account);
paymentsRequest.stream(new EventListener <OperationResponse>(){
@Override
public void onEvent(OperationResponse payment) {
if (payment instanceof PaymentOperationResponse) {
if (((PaymentOperationResponse) payment).getTo().equals(account)) {
return;
}
String amount = ((PaymentOperationResponse) payment).getAmount();
Asset asset = ((PaymentOperationResponse) payment).getAsset();
String assetName;
if (asset.equals(new AssetTypeNative())) {
assetName = "lumens";
} else {
StringBuilder assetNameBuilder = new StringBuilder();
assetNameBuilder.append(((AssetTypeCreditAlphaNum) asset).getCode());
assetNameBuilder.append(":");
assetNameBuilder.append(((AssetTypeCreditAlphaNum) asset).getIssuer().getAccountId());
assetName = assetNameBuilder.toString();
}
StringBuilder output = new StringBuilder();
output.append(amount);
output.append(" ");
output.append(assetName);
output.append(" from ");
output.append(((PaymentOperationResponse) payment).getFrom().getAccountId());
System.out.println(output.toString());
}
}
});
/**
* option 1
*
*/
/*try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
/**
* option 2
*/
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
输出如下所示:
10.0000000 lumens from GC2BKLYOOYPDEFJKLKY6FNNRQMGFLVHJKQRGNSSRRGSMPGF32LHCQVGF many line like this