Java Spring JPA必须至少请求两次才能获取数据
我接到这个奇怪的案子。所以,无论何时我想得到付款,我都必须用邮递员至少发送两次请求。请看一下我的代码: UserAPI.javaJava Spring JPA必须至少请求两次才能获取数据,java,spring,jpa,Java,Spring,Jpa,我接到这个奇怪的案子。所以,无论何时我想得到付款,我都必须用邮递员至少发送两次请求。请看一下我的代码: UserAPI.java @RestController @RequestMapping("/api") public class UserAPI { APISend send = new APISend(); public final APIReceive recv = new APIReceive(); @GetMapping("/
@RestController
@RequestMapping("/api")
public class UserAPI {
APISend send = new APISend();
public final APIReceive recv = new APIReceive();
@GetMapping("/home/{username}")
public ResponseEntity<?> getData(@PathVariable("username") String username) {
try {
send.getPayment(username);
return new ResponseEntity<>(recv.getPayment(), HttpStatus.OK);
} catch (Exception e) {
System.out.println("ERROR GET DATA: " + e);
return new ResponseEntity<>("Data not found.", HttpStatus.NOT_FOUND);
}
}
}
public class APISend {
public void getPayment(String username) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection con = factory.newConnection();
Channel channel = con.createChannel()) {
channel.queueDeclare("getPayment", false, false, false, null);
channel.basicPublish("", "getPayment", null, username.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] SEND GET DATA SALDO '" + username + "'");
}
}
}
public void con() {
this.entityManager = Persistence
.createEntityManagerFactory("user-unit")
.createEntityManager();
naDao = new UserDAO(entityManager);
try {
entityManager.getTransaction().begin();
} catch (IllegalStateException e) {
entityManager.getTransaction().rollback();
}
}
public void com() {
try {
entityManager.getTransaction().commit();
entityManager.close();
} catch (IllegalStateException e) {
entityManager.getTransaction().rollback();
}
}
public void connectRabbitMQ() throws IOException, TimeoutException {
factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
}
public void getSaldo() {
try {
connectRabbitMQ();
channel = connection.createChannel();
channel.queueDeclare("getPayment", false, false, false, null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String username = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + username + "'");
con();
int res = naDao.checkUser(username);
String payment = "";
if (res != 0){
User nb = new User();
nb.setId_user(res);
int money = naDao.getPayment(nb);
payment = String.valueOf(money);
} else {
payment="0";
}
try {
send.sendToAPI(payment);
} catch (Exception e) {
System.out.println("ERROR SEND TO API GET PAYMENT: " + e);
}
com();
};
channel.basicConsume("getPayment", true, deliverCallback, consumerTag -> { });
} catch (Exception e) {
System.out.println("ERROR GET PAYMENT = " + e);
}
}
public int checkUser(String username) {
String select = "SELECT id FROM User WHERE username=:username AND loginStatus=:loginStatus";
Query q = entityManager.createQuery(select);
q.setParameter("username", username);
q.setParameter("loginStatus", "true");
if (q.getResultList().size() != 0) {
return (int) q.getResultList().get(0);
} else {
return q.getResultList().size();
}
}
public int getPayment(User nb) {
String query = "SELECT n.payment FROM User n WHERE n.id=:id";
Query q = entityManager.createQuery(query);
q.setParameter("id", nb.getId());
if (q.getResultList().size()!=0){
return (int)q.getResultList().get(0);
} else {
return q.getResultList().size();
}
}
public void sendToAPI (String message) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare("paymentFromDB", false, false, false, null);
channel.basicPublish("", "paymentFromDB", null, message.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] Sent '" + message + "'");
} catch (Exception e) {
System.out.println("ERROR SEND TO API" + e);
}
}
public class APIReceive {
protected String message = "";
private String paymentResponse = "";
public void setMessage(String message) {
this.message = message;
}
public String getpaymentResponse() {
return paymentResponse;
}
public void setpaymentResponse(String paymentResponse) {
this.paymentResponse = paymentResponse;
}
public String getPayment() throws IOException, TimeoutException {
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("paymentFromDB", false, false, false, null);
System.out.println(" [*] Waiting for messages from database");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Payment Received '" + message + "'");
this.message = message;
};
channel.basicConsume("paymentFromDB", true, deliverCallback, consumerTag -> { });
TimeUnit.SECONDS.sleep(2);
if (!this.message.equals("0")) {
JSONObject object = new JSONObject();
object.put("response", 200);
object.put("status", "Success");
object.put("message", "Success Check Payment");
object.put("Saldo", this.message);
saldoResponse = object.toJSONString();
} else {
JSONObject object = new JSONObject();
object.put("response", 400);
object.put("status", "Error");
object.put("message", "Error Getting Payment!");
saldoResponse = object.toJSONString();
}
} catch (Exception e) {
System.out.println("ERROR GET SALDO APIRecv: " + e);
}
System.out.println("payment response: "+this.getPaymentResponse());
return this.getPaymentResponse();
}
public String getMessage() {
return message;
}
}
DatabaseReceiver.java
@RestController
@RequestMapping("/api")
public class UserAPI {
APISend send = new APISend();
public final APIReceive recv = new APIReceive();
@GetMapping("/home/{username}")
public ResponseEntity<?> getData(@PathVariable("username") String username) {
try {
send.getPayment(username);
return new ResponseEntity<>(recv.getPayment(), HttpStatus.OK);
} catch (Exception e) {
System.out.println("ERROR GET DATA: " + e);
return new ResponseEntity<>("Data not found.", HttpStatus.NOT_FOUND);
}
}
}
public class APISend {
public void getPayment(String username) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection con = factory.newConnection();
Channel channel = con.createChannel()) {
channel.queueDeclare("getPayment", false, false, false, null);
channel.basicPublish("", "getPayment", null, username.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] SEND GET DATA SALDO '" + username + "'");
}
}
}
public void con() {
this.entityManager = Persistence
.createEntityManagerFactory("user-unit")
.createEntityManager();
naDao = new UserDAO(entityManager);
try {
entityManager.getTransaction().begin();
} catch (IllegalStateException e) {
entityManager.getTransaction().rollback();
}
}
public void com() {
try {
entityManager.getTransaction().commit();
entityManager.close();
} catch (IllegalStateException e) {
entityManager.getTransaction().rollback();
}
}
public void connectRabbitMQ() throws IOException, TimeoutException {
factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
}
public void getSaldo() {
try {
connectRabbitMQ();
channel = connection.createChannel();
channel.queueDeclare("getPayment", false, false, false, null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String username = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + username + "'");
con();
int res = naDao.checkUser(username);
String payment = "";
if (res != 0){
User nb = new User();
nb.setId_user(res);
int money = naDao.getPayment(nb);
payment = String.valueOf(money);
} else {
payment="0";
}
try {
send.sendToAPI(payment);
} catch (Exception e) {
System.out.println("ERROR SEND TO API GET PAYMENT: " + e);
}
com();
};
channel.basicConsume("getPayment", true, deliverCallback, consumerTag -> { });
} catch (Exception e) {
System.out.println("ERROR GET PAYMENT = " + e);
}
}
public int checkUser(String username) {
String select = "SELECT id FROM User WHERE username=:username AND loginStatus=:loginStatus";
Query q = entityManager.createQuery(select);
q.setParameter("username", username);
q.setParameter("loginStatus", "true");
if (q.getResultList().size() != 0) {
return (int) q.getResultList().get(0);
} else {
return q.getResultList().size();
}
}
public int getPayment(User nb) {
String query = "SELECT n.payment FROM User n WHERE n.id=:id";
Query q = entityManager.createQuery(query);
q.setParameter("id", nb.getId());
if (q.getResultList().size()!=0){
return (int)q.getResultList().get(0);
} else {
return q.getResultList().size();
}
}
public void sendToAPI (String message) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare("paymentFromDB", false, false, false, null);
channel.basicPublish("", "paymentFromDB", null, message.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] Sent '" + message + "'");
} catch (Exception e) {
System.out.println("ERROR SEND TO API" + e);
}
}
public class APIReceive {
protected String message = "";
private String paymentResponse = "";
public void setMessage(String message) {
this.message = message;
}
public String getpaymentResponse() {
return paymentResponse;
}
public void setpaymentResponse(String paymentResponse) {
this.paymentResponse = paymentResponse;
}
public String getPayment() throws IOException, TimeoutException {
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("paymentFromDB", false, false, false, null);
System.out.println(" [*] Waiting for messages from database");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Payment Received '" + message + "'");
this.message = message;
};
channel.basicConsume("paymentFromDB", true, deliverCallback, consumerTag -> { });
TimeUnit.SECONDS.sleep(2);
if (!this.message.equals("0")) {
JSONObject object = new JSONObject();
object.put("response", 200);
object.put("status", "Success");
object.put("message", "Success Check Payment");
object.put("Saldo", this.message);
saldoResponse = object.toJSONString();
} else {
JSONObject object = new JSONObject();
object.put("response", 400);
object.put("status", "Error");
object.put("message", "Error Getting Payment!");
saldoResponse = object.toJSONString();
}
} catch (Exception e) {
System.out.println("ERROR GET SALDO APIRecv: " + e);
}
System.out.println("payment response: "+this.getPaymentResponse());
return this.getPaymentResponse();
}
public String getMessage() {
return message;
}
}
UserDAO.java
@RestController
@RequestMapping("/api")
public class UserAPI {
APISend send = new APISend();
public final APIReceive recv = new APIReceive();
@GetMapping("/home/{username}")
public ResponseEntity<?> getData(@PathVariable("username") String username) {
try {
send.getPayment(username);
return new ResponseEntity<>(recv.getPayment(), HttpStatus.OK);
} catch (Exception e) {
System.out.println("ERROR GET DATA: " + e);
return new ResponseEntity<>("Data not found.", HttpStatus.NOT_FOUND);
}
}
}
public class APISend {
public void getPayment(String username) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection con = factory.newConnection();
Channel channel = con.createChannel()) {
channel.queueDeclare("getPayment", false, false, false, null);
channel.basicPublish("", "getPayment", null, username.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] SEND GET DATA SALDO '" + username + "'");
}
}
}
public void con() {
this.entityManager = Persistence
.createEntityManagerFactory("user-unit")
.createEntityManager();
naDao = new UserDAO(entityManager);
try {
entityManager.getTransaction().begin();
} catch (IllegalStateException e) {
entityManager.getTransaction().rollback();
}
}
public void com() {
try {
entityManager.getTransaction().commit();
entityManager.close();
} catch (IllegalStateException e) {
entityManager.getTransaction().rollback();
}
}
public void connectRabbitMQ() throws IOException, TimeoutException {
factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
}
public void getSaldo() {
try {
connectRabbitMQ();
channel = connection.createChannel();
channel.queueDeclare("getPayment", false, false, false, null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String username = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + username + "'");
con();
int res = naDao.checkUser(username);
String payment = "";
if (res != 0){
User nb = new User();
nb.setId_user(res);
int money = naDao.getPayment(nb);
payment = String.valueOf(money);
} else {
payment="0";
}
try {
send.sendToAPI(payment);
} catch (Exception e) {
System.out.println("ERROR SEND TO API GET PAYMENT: " + e);
}
com();
};
channel.basicConsume("getPayment", true, deliverCallback, consumerTag -> { });
} catch (Exception e) {
System.out.println("ERROR GET PAYMENT = " + e);
}
}
public int checkUser(String username) {
String select = "SELECT id FROM User WHERE username=:username AND loginStatus=:loginStatus";
Query q = entityManager.createQuery(select);
q.setParameter("username", username);
q.setParameter("loginStatus", "true");
if (q.getResultList().size() != 0) {
return (int) q.getResultList().get(0);
} else {
return q.getResultList().size();
}
}
public int getPayment(User nb) {
String query = "SELECT n.payment FROM User n WHERE n.id=:id";
Query q = entityManager.createQuery(query);
q.setParameter("id", nb.getId());
if (q.getResultList().size()!=0){
return (int)q.getResultList().get(0);
} else {
return q.getResultList().size();
}
}
public void sendToAPI (String message) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare("paymentFromDB", false, false, false, null);
channel.basicPublish("", "paymentFromDB", null, message.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] Sent '" + message + "'");
} catch (Exception e) {
System.out.println("ERROR SEND TO API" + e);
}
}
public class APIReceive {
protected String message = "";
private String paymentResponse = "";
public void setMessage(String message) {
this.message = message;
}
public String getpaymentResponse() {
return paymentResponse;
}
public void setpaymentResponse(String paymentResponse) {
this.paymentResponse = paymentResponse;
}
public String getPayment() throws IOException, TimeoutException {
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("paymentFromDB", false, false, false, null);
System.out.println(" [*] Waiting for messages from database");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Payment Received '" + message + "'");
this.message = message;
};
channel.basicConsume("paymentFromDB", true, deliverCallback, consumerTag -> { });
TimeUnit.SECONDS.sleep(2);
if (!this.message.equals("0")) {
JSONObject object = new JSONObject();
object.put("response", 200);
object.put("status", "Success");
object.put("message", "Success Check Payment");
object.put("Saldo", this.message);
saldoResponse = object.toJSONString();
} else {
JSONObject object = new JSONObject();
object.put("response", 400);
object.put("status", "Error");
object.put("message", "Error Getting Payment!");
saldoResponse = object.toJSONString();
}
} catch (Exception e) {
System.out.println("ERROR GET SALDO APIRecv: " + e);
}
System.out.println("payment response: "+this.getPaymentResponse());
return this.getPaymentResponse();
}
public String getMessage() {
return message;
}
}
DatabaseSender.java
@RestController
@RequestMapping("/api")
public class UserAPI {
APISend send = new APISend();
public final APIReceive recv = new APIReceive();
@GetMapping("/home/{username}")
public ResponseEntity<?> getData(@PathVariable("username") String username) {
try {
send.getPayment(username);
return new ResponseEntity<>(recv.getPayment(), HttpStatus.OK);
} catch (Exception e) {
System.out.println("ERROR GET DATA: " + e);
return new ResponseEntity<>("Data not found.", HttpStatus.NOT_FOUND);
}
}
}
public class APISend {
public void getPayment(String username) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection con = factory.newConnection();
Channel channel = con.createChannel()) {
channel.queueDeclare("getPayment", false, false, false, null);
channel.basicPublish("", "getPayment", null, username.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] SEND GET DATA SALDO '" + username + "'");
}
}
}
public void con() {
this.entityManager = Persistence
.createEntityManagerFactory("user-unit")
.createEntityManager();
naDao = new UserDAO(entityManager);
try {
entityManager.getTransaction().begin();
} catch (IllegalStateException e) {
entityManager.getTransaction().rollback();
}
}
public void com() {
try {
entityManager.getTransaction().commit();
entityManager.close();
} catch (IllegalStateException e) {
entityManager.getTransaction().rollback();
}
}
public void connectRabbitMQ() throws IOException, TimeoutException {
factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
}
public void getSaldo() {
try {
connectRabbitMQ();
channel = connection.createChannel();
channel.queueDeclare("getPayment", false, false, false, null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String username = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + username + "'");
con();
int res = naDao.checkUser(username);
String payment = "";
if (res != 0){
User nb = new User();
nb.setId_user(res);
int money = naDao.getPayment(nb);
payment = String.valueOf(money);
} else {
payment="0";
}
try {
send.sendToAPI(payment);
} catch (Exception e) {
System.out.println("ERROR SEND TO API GET PAYMENT: " + e);
}
com();
};
channel.basicConsume("getPayment", true, deliverCallback, consumerTag -> { });
} catch (Exception e) {
System.out.println("ERROR GET PAYMENT = " + e);
}
}
public int checkUser(String username) {
String select = "SELECT id FROM User WHERE username=:username AND loginStatus=:loginStatus";
Query q = entityManager.createQuery(select);
q.setParameter("username", username);
q.setParameter("loginStatus", "true");
if (q.getResultList().size() != 0) {
return (int) q.getResultList().get(0);
} else {
return q.getResultList().size();
}
}
public int getPayment(User nb) {
String query = "SELECT n.payment FROM User n WHERE n.id=:id";
Query q = entityManager.createQuery(query);
q.setParameter("id", nb.getId());
if (q.getResultList().size()!=0){
return (int)q.getResultList().get(0);
} else {
return q.getResultList().size();
}
}
public void sendToAPI (String message) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare("paymentFromDB", false, false, false, null);
channel.basicPublish("", "paymentFromDB", null, message.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] Sent '" + message + "'");
} catch (Exception e) {
System.out.println("ERROR SEND TO API" + e);
}
}
public class APIReceive {
protected String message = "";
private String paymentResponse = "";
public void setMessage(String message) {
this.message = message;
}
public String getpaymentResponse() {
return paymentResponse;
}
public void setpaymentResponse(String paymentResponse) {
this.paymentResponse = paymentResponse;
}
public String getPayment() throws IOException, TimeoutException {
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("paymentFromDB", false, false, false, null);
System.out.println(" [*] Waiting for messages from database");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Payment Received '" + message + "'");
this.message = message;
};
channel.basicConsume("paymentFromDB", true, deliverCallback, consumerTag -> { });
TimeUnit.SECONDS.sleep(2);
if (!this.message.equals("0")) {
JSONObject object = new JSONObject();
object.put("response", 200);
object.put("status", "Success");
object.put("message", "Success Check Payment");
object.put("Saldo", this.message);
saldoResponse = object.toJSONString();
} else {
JSONObject object = new JSONObject();
object.put("response", 400);
object.put("status", "Error");
object.put("message", "Error Getting Payment!");
saldoResponse = object.toJSONString();
}
} catch (Exception e) {
System.out.println("ERROR GET SALDO APIRecv: " + e);
}
System.out.println("payment response: "+this.getPaymentResponse());
return this.getPaymentResponse();
}
public String getMessage() {
return message;
}
}
apirecever.java
@RestController
@RequestMapping("/api")
public class UserAPI {
APISend send = new APISend();
public final APIReceive recv = new APIReceive();
@GetMapping("/home/{username}")
public ResponseEntity<?> getData(@PathVariable("username") String username) {
try {
send.getPayment(username);
return new ResponseEntity<>(recv.getPayment(), HttpStatus.OK);
} catch (Exception e) {
System.out.println("ERROR GET DATA: " + e);
return new ResponseEntity<>("Data not found.", HttpStatus.NOT_FOUND);
}
}
}
public class APISend {
public void getPayment(String username) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection con = factory.newConnection();
Channel channel = con.createChannel()) {
channel.queueDeclare("getPayment", false, false, false, null);
channel.basicPublish("", "getPayment", null, username.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] SEND GET DATA SALDO '" + username + "'");
}
}
}
public void con() {
this.entityManager = Persistence
.createEntityManagerFactory("user-unit")
.createEntityManager();
naDao = new UserDAO(entityManager);
try {
entityManager.getTransaction().begin();
} catch (IllegalStateException e) {
entityManager.getTransaction().rollback();
}
}
public void com() {
try {
entityManager.getTransaction().commit();
entityManager.close();
} catch (IllegalStateException e) {
entityManager.getTransaction().rollback();
}
}
public void connectRabbitMQ() throws IOException, TimeoutException {
factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
}
public void getSaldo() {
try {
connectRabbitMQ();
channel = connection.createChannel();
channel.queueDeclare("getPayment", false, false, false, null);
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String username = new String(delivery.getBody(), StandardCharsets.UTF_8);
System.out.println(" [x] Received '" + username + "'");
con();
int res = naDao.checkUser(username);
String payment = "";
if (res != 0){
User nb = new User();
nb.setId_user(res);
int money = naDao.getPayment(nb);
payment = String.valueOf(money);
} else {
payment="0";
}
try {
send.sendToAPI(payment);
} catch (Exception e) {
System.out.println("ERROR SEND TO API GET PAYMENT: " + e);
}
com();
};
channel.basicConsume("getPayment", true, deliverCallback, consumerTag -> { });
} catch (Exception e) {
System.out.println("ERROR GET PAYMENT = " + e);
}
}
public int checkUser(String username) {
String select = "SELECT id FROM User WHERE username=:username AND loginStatus=:loginStatus";
Query q = entityManager.createQuery(select);
q.setParameter("username", username);
q.setParameter("loginStatus", "true");
if (q.getResultList().size() != 0) {
return (int) q.getResultList().get(0);
} else {
return q.getResultList().size();
}
}
public int getPayment(User nb) {
String query = "SELECT n.payment FROM User n WHERE n.id=:id";
Query q = entityManager.createQuery(query);
q.setParameter("id", nb.getId());
if (q.getResultList().size()!=0){
return (int)q.getResultList().get(0);
} else {
return q.getResultList().size();
}
}
public void sendToAPI (String message) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare("paymentFromDB", false, false, false, null);
channel.basicPublish("", "paymentFromDB", null, message.getBytes(StandardCharsets.UTF_8));
System.out.println(" [x] Sent '" + message + "'");
} catch (Exception e) {
System.out.println("ERROR SEND TO API" + e);
}
}
public class APIReceive {
protected String message = "";
private String paymentResponse = "";
public void setMessage(String message) {
this.message = message;
}
public String getpaymentResponse() {
return paymentResponse;
}
public void setpaymentResponse(String paymentResponse) {
this.paymentResponse = paymentResponse;
}
public String getPayment() throws IOException, TimeoutException {
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("paymentFromDB", false, false, false, null);
System.out.println(" [*] Waiting for messages from database");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
System.out.println(" [x] Payment Received '" + message + "'");
this.message = message;
};
channel.basicConsume("paymentFromDB", true, deliverCallback, consumerTag -> { });
TimeUnit.SECONDS.sleep(2);
if (!this.message.equals("0")) {
JSONObject object = new JSONObject();
object.put("response", 200);
object.put("status", "Success");
object.put("message", "Success Check Payment");
object.put("Saldo", this.message);
saldoResponse = object.toJSONString();
} else {
JSONObject object = new JSONObject();
object.put("response", 400);
object.put("status", "Error");
object.put("message", "Error Getting Payment!");
saldoResponse = object.toJSONString();
}
} catch (Exception e) {
System.out.println("ERROR GET SALDO APIRecv: " + e);
}
System.out.println("payment response: "+this.getPaymentResponse());
return this.getPaymentResponse();
}
public String getMessage() {
return message;
}
}
我得到的答复是:
程序首次运行
[x] Waiting for messages from database
Payment response: {"response":200,"Payment":"","message":"Success Check Payment","status":"Success"}
第一个请求:
[x] SEND GET DATA Payment 'user1'
[x] Waiting for messages from database
[x] Payment Received '5000000'
Payment response: {"response":200,"Payment":"","message":"Success Check Payment","status":"Success"}
第二项请求:
[x] SEND GET DATA Payment 'user1'
[x] Waiting for messages from database
[x] Payment Received '5000000'
Payment response: {"response":200,"Payment":"5000000","message":"Success Check Payment","status":"Success"}
当我将protected String message=”“
设置为protected String message
时,我将得到错误java.lang.NullPointerException:无法调用“String.equals(Object)”,因为“this.message”为null
有人能帮帮我吗?请提供一个最简单的代码示例来说明您的问题。没有人会去挖掘你发布的大量代码。请提供一个最小的代码示例来说明你的问题。没有人会去挖掘你发布的大量代码。