在lagom项目中,我试图使用访问器,但是我得到了一个错误。
java.util.concurrent.completionexception:java.lang.runtimeexception:在java.util.concurrent.completablefuture.encodethrowable(completablefuture)处为访问器testaccessor准备查询时出错。java:273)在java.util.concurrent.completablefuture.unicomposestage(completablefuture。java:991)在java.util.concurrent.completablefuture.thencose(completablefuture。java:2124)在java.util.concurrent.completablefuture.thencose(completablefuture。java:110)在org.test.impl.persist.cassandrarepository.getall(cassandrarepository。java:98)在org.test.impl.workers.worker.lambda$testmessage$2(worker。java:91)在java.util.concurrent.completablefuture.unicompose(completablefuture。java:952)位于java.util.concurrent.completablefuture$unicompose.tryfire(completablefuture。java:926)在java.util.concurrent.completablefuture.postcomplete(completablefuture。java:474)位于java.util.concurrent.completablefuture.complete(completablefuture。java:1962)在com.spotify.futures.listenabletocompletablefuturewrapper.onsuccess(listenabletocompletablefuturewrapper)。java:49)在com.google.common.util.concurrent.futures$callbacklistener.run(futures。java:1237)在com.google.common.util.concurrent.moreexecutors$directexecutor.execute(moreexecutors。java:399)在com.google.common.util.concurrent.abstractfuture.executelistener(抽象未来。java:911)在com.google.common.util.concurrent.abstractfuture.complete(abstractfuture。java:822)在com.google.common.util.concurrent.abstractfuture.set(abstractfuture。java:664)在com.google.common.util.concurrent.abstracttransformfuture$transformfuture.setresult(abstracttransformfuture。java:245)在com.google.common.util.concurrent.abstracttransformfuture.run(abstracttransformfuture。java:177)在com.google.common.util.concurrent.moreexecutors$directexecutor.execute(moreexecutors。java:399)在com.google.common.util.concurrent.abstractfuture.executelistener(抽象未来。java:911)在com.google.common.util.concurrent.abstractfuture.complete(abstractfuture。java:822)在com.google.common.util.concurrent.abstractfuture.set(abstractfuture。java:664)在com.datastax.driver.core.defaultresultsetfuture.inite(defaultresultsetfuture。java:174)在com.datastax.driver.core.requesthandler.setfinalresult(requesthandler。java:198)在com.datastax.driver.core.requesthandler.access$2600(requesthandler。java:50)在com.datastax.driver.core.requesthandler$投机执行.setfinalresult(requesthandler。java:852)在com.datastax.driver.core.requesthandler$投机执行.initiable(requesthandler。java:556)在com.datastax.driver.core.connection$dispatcher.channelread0(连接。java:1087)在com.datastax.driver.core.connection$dispatcher.channelread0(连接。java:1010)在io.netty.channel.simplechannelinboundhandler.channelread(simplechannelinboundhandler。java:105)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:362)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:348)在io.netty.channel.abstractchannelhandlercontext.firechannelread(abstractchannelhandlercontext。java:340)在io.netty.handler.timeout.idlestatehandler.channelread(idlestatehandler。java:286)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:362)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:348)在io.netty.channel.abstractchannelhandlercontext.firechannelread(abstractchannelhandlercontext。java:340)在io.netty.handler.codec.messagetomessagedecoder.channelread(messagetomessagedecoder。java:102)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:362)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:348)在io.netty.channel.abstractchannelhandlercontext.firechannelread(abstractchannelhandlercontext。java:340)在io.netty.handler.codec.bytetomessagedecoder.firechannelread(bytetomessagedecoder。java:310)在io.netty.handler.codec.bytetomessagedecoder.channelread(bytetomessagedecoder。java:284)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:362)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:348)在io.netty.channel.abstractchannelhandlercontext.firechannelread(abstractchannelhandlercontext。java:340)在io.netty.channel.channelinboundhandleradapter.channelread(channelinboundhandleradapter。java:86)在com.datastax.driver.core.inboundtrafficmeter.channelread(inboundtrafficmeter。java:38)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:362)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:348)在io.netty.channel.abstractchannelhandlercontext.firechannelread(abstractchannelhandlercontext。java:340)在io.netty.channel.defaultchannelpipeline$headcontext.channelread(defaultchannelpipeline。java:1434)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:362)在io.netty.channel.abstractchannelhandlercontext.invokechannelread(abstractchannelhandlercontext。java:348)在io.netty.channel.defaultchannelpipeline.firechannelread(defaultchannelpipeline。java:965)在io.netty.channel.nio.abstractniobytechannel$niobytueunsafe.read(abstractniobytechannel。java:163)在io.netty.channel.nio.nioeventloop.processselectedkey(nioeventloop。java:647)在io.netty.channel.nio.nioeventloop.processselectedkeysoptimized(nioeventloop。java:582)在io.netty.channel.nio.nioeventloop.processselectedkeys(nioeventloop。java:499)在io.netty.channel.nio.nioeventloop.run(nioeventloop。java:461)在io.netty.util.concurrent.singlethreadeventexecutor$5.run(singlethreadeventexecutor。java:884)在io.netty.util.concurrent.fastthreadlocalrunnable.run(fastthreadlocalrunnable。java:30)在java.lang.thread.run(线程。java:748)原因:java.lang.runtimeexception:为位于的访问器testaccessor准备查询时出错com.datastax.driver.mapping.accessormapper.prepare(accessormapper。java:61)在com.datastax.driver.mapping.mappingmanager.getaccessor(mappingmanager。java:335)在com.datastax.driver.mapping.mappingmanager.createaccessor(mappingmanager。java:297)在org.test.impl.persist.cassandrarepository.lambda$getall$5(cassandrarepository。java:99)在java.util.concurrent.completablefuture.unicomposestage(completablefuture。java:981) ... 61由于以下原因而忽略的公共帧:java.util.concurrent.executionexception:com.datastax.driver.core.exceptions.operationtimedoutexception:[localhost/127.0.0.1:9042]在com.google.common.util.concurrent.abstractfuture.getdonevalue(abstractfuture)等待服务器响应时超时。java:503)在com.google.common.util.concurrent.abstractfuture.get(抽象未来。java:482)在com.google.common.util.concurrent.abstractfuture$trustedfuture.get(abstractfuture。java:79)在com.datastax.driver.mapping.accessormapper.prepare(accessormapper。java:57) ... 65由于以下原因而忽略的公共帧:com.datastax.driver.core.exceptions.operationtimedoutexception:[localhost/127.0.0.1:9042]在com.datastax.driver.core.requesthandler$投机执行.ontimeout(requesthandler)等待服务器响应时超时。java:825)在com.datastax.driver.core.connection$responsehandler$1.run(连接。java:1392)在io.netty.util.hashedweeltimer$hashedweeltimeout.expire(hashedweeltimer。java:668)在io.netty.util.hashedwheeltimer$hashedwheelbucket.expiretimeouts(hashedwheeltimer。java:743)在io.netty.util.hashedwheeltimer$worker.run(hashedwheeltimer。java:471) ... 省略2个公共框架
存取器
@Accessor
public interface TestAccessor {
@Query("SELECT * FROM test_table")
Result<TestTable> getAll();
}
使用
TestAccessor acc = mappingManager.createAccessor(TestAccessor.class);
Result<TestTable> channels = acc.getAll();
提供会话和Map器。
public class Module extends AbstractModule implements ServiceGuiceSupport, AkkaGuiceSupport {
private static CompletableFuture<Void> codecRegistrationPromise = new CompletableFuture<>();
@Override
protected void configure() {
bindService(TestService.class, TestServiceImpl.class);
}
@Provides
@Singleton
public CompletionStage<MappingManager> provideMappingManager(CompletionStage<Session> session) {
return session.thenApply(MappingManager::new));
}
@Provides
public CompletionStage<Cluster> provideCassandraCluster(CompletionStage<Session> session) {
return session.thenApply(Session::getCluster);
}
@Provides
@Singleton
public CompletionStage<Session> provideCassandraSession(CassandraSession sessionWrapper) {
CompletableFuture<Session> future = new CompletableFuture<>();
completeWithSession(sessionWrapper, future);
return future;
}
private void completeWithSession(CassandraSession sessionWrapper, CompletableFuture<Session> future) {
sessionWrapper.underlying().whenComplete((session, throwable) -> {
if (throwable != null) {
if (throwable instanceof NoServiceLocatorException) {
log.error("Failed to obtain Cassandra session, try again", throwable);
completeWithSession(sessionWrapper, future);
} else {
log.error("Failed to obtain Cassandra session", throwable);
future.completeExceptionally(throwable);
}
} else {
log.info("Cassandra session obtained");
future.complete(session);
}
});
}
}
在某些类中的用法
@Inject
public CassandraRepository(CompletionStage<MappingManager> mappingManagerPromise, ActorSystem system) {
this.mappingManagerPromise = mappingManagerPromise;
this.system = system;
}
mappingManagerPromise.thenApply(mappingManager -> {
TestAccessor acc = mappingManager.createAccessor(TestAccessor.class);
Result<TestTable> channels = acc.getAll();
})
如果我使用mapper.getasync,一切正常。
Mapper<TestTable> mapper = mappingManager.mapper(TestTable.class);
return toCompletableFuture(mapper.getAsync(id));
暂无答案!
目前还没有任何答案,快来回答吧!