我需要以下代码来同步运行。
这里是我的家庭控制器类的相关部分
/*
* Pulls up beancoin blockchain on startup.
*
* If no beancoin exists, create one and populate it with initial values
*/
@ModelAttribute("blockchain")
public Blockchain addBlockchain() throws NoSuchAlgorithmException, InterruptedException {
try {
Blockchain blockchain = blockchainApp.getBlockchainService("beancoin");
pnapp = new PubNubApp(blockchain);
System.out.println("Pulling up your beancoin from our records");
return blockchain;
} catch (NoResultException e) {
System.err.println("Creating new beancoin");
Blockchain blockchain = blockchainApp.newBlockchainService("beancoin");
// this seems to be going async and I need it to be sync (based on web render). What could be cause?
Initializer.loadBC("beancoin");
return blockchain;
}
}
上述方法在流方面运行良好,并正确地填充了本地数据库。但是,第一次在网页上显示的只是genesis\块,然后我必须启动和停止服务器,然后它会显示所有来自 Initializer.loadBC("beancoin");
打电话。我不知道是什么原因导致它不同步。
这是你的名字 Initailizer.loadBC("beancoin");
斯塔克,我觉得会有帮助的。
电话:
public static void loadBC(String nameOfBlockchain) {
BlockchainService blockchainApp = new BlockchainService();
blockchainApp.addBlockService(nameOfBlockchain, new String[] { "Dance", "The", "Quickstep" });
blockchainApp.addBlockService(nameOfBlockchain, new String[] { "Dance", "The", "Waltz" });
blockchainApp.addBlockService(nameOfBlockchain, new String[] { "Dance", "The", "Tango" });
blockchainApp.addBlockService(nameOfBlockchain, new String[] { "Dance", "The", "Samba" });
blockchainApp.addBlockService(nameOfBlockchain, new String[] { "Dance", "With", "Us", "America" });
}
电话:
public boolean addBlockService(String name, String[] data) {
return blockchainD.addBlock(name, data);
}
电话:
@Override
public boolean addBlock(String name, String[] data) {
this.connect();
Query query = em.createQuery("select b from Blockchain b where b.instance_name = :name");
query.setParameter("name", name);
Blockchain blockchain = (Blockchain) query.getSingleResult();
try {
em.getTransaction().begin();
Block new_block = blockchain.add_block(data);
em.persist(new_block);
em.getTransaction().commit();
this.disconnect();
System.out.println("Returning true");
return true;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
我想这应该是你需要的全部信息了。
它没有抛出任何错误。它只是与页面不同步,因此如果我刷新所有数据库表(从头开始)并启动服务器,将显示以下内容:
只有这个。
然而,数据库有我需要的一切,如果我启动和停止服务器,并转到同一页,我得到这个(如预期的)。
这不仅仅是一个展示问题,尽管这很重要。我需要我的控制器是最新的,以便它可以与其他节点进行通信,并适当地报告链和更新链,如有必要。我的代码中似乎有某种东西隐式地告诉jvm它可以并发运行,或者在不同的线程上运行??请帮我理解
编辑:当我把控制器代码改成这样时,它看起来仍然是异步的。我肯定给它足够的时间,所以一定有一个概念我不明白。
@ModelAttribute("blockchain")
public Blockchain addBlockchain() throws NoSuchAlgorithmException, InterruptedException {
try {
Blockchain blockchain = blockchainApp.getBlockchainService("beancoin");
pnapp = new PubNubApp(blockchain);
System.out.println("Pulling up your beancoin from our records");
return blockchain;
} catch (NoResultException e) {
System.err.println("Creating new beancoin");
Blockchain blockchain = blockchainApp.newBlockchainService("beancoin");
Thread.sleep(15000);
// this seems to be going async and I need it to be sync (based on web render).
// What could be cause?
blockchainApp.addBlockService("beancoin", new String[] { "Dance", "The", "Quickstep" });
blockchainApp.addBlockService("beancoin", new String[] { "Dance", "The", "Waltz" });
blockchainApp.addBlockService("beancoin", new String[] { "Dance", "The", "Tango" });
blockchainApp.addBlockService("beancoin", new String[] { "Dance", "The", "Samba" });
blockchainApp.addBlockService("beancoin", new String[] { "Dance", "With", "Us", "America" });
return blockchain;
}
}
1条答案
按热度按时间mpgws1up1#
我有点傻,所以我很高兴没人回答这个问题。它与同步或异步无关。
最好用代码来解释我需要做什么,而不是用文字:
我确实向数据库中添加了每个调用,但没有重新查询所需的示例。很简单,所以在某种程度上它是异步的,但不是由于线程或任何类似的性质。只是人的疏忽。