在lagom项目中,我尝试使用 Accessors 但是我得到一个错误。
java.util.concurrent.CompletionException:java.lang.RuntimeException:为访问器TestAccessor准备查询时出错 在java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273) 在java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:991) 在java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2124) 在java.util.concurrent.CompletableFuture.thenCompose(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(AbstractFuture.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(AbstractFuture.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.onSet(DefaultResultSetFuture.java:174) 在com.datastax.driver.core.RequestHandler.setFinalResult(RequestHandler.java:198) at com.datastax.driver.core.RequestHandler.access $ 2600(RequestHandler.java:50) 在com.datastax.driver.core.RequestHandler $ SpeculativeExecution.setFinalResult(RequestHandler.java:852) 在com.datastax.driver.core.RequestHandler $ SpeculativeExecution.onSet(RequestHandler.java:556) 在com.datastax.driver.core.Connection $ Dispatcher.channelRead0(Connection.java:1087) 在com.datastax.driver.core.Connection $ Dispatcher.channelRead0(Connection.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 $ NioByteUnsafe.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(Thread.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(AbstractFuture.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 $ SpeculativeExecution.onTimeout(RequestHandler.java:825) 在com.datastax.driver.core.Connection $ ResponseHandler $ 1.run(Connection.java:1392) 在io.netty.util.HashedWheelTimer $ HashedWheelTimeout.expire(HashedWheelTimer.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();
提供会话和映射器。
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));