I'm trying to create a simple restful api using play and scala. I've been able to create some basic routes, get it to send back json responses, and validate json sent in a POST. Now I want to add persistence to a postgres database, which is where it's starting to break down.
I started off by adding the db info to my application.conf, creating an evolution to create a table. I then attempted to hit my GET endpoint to see if it runs the evolution, but I instead get an error. Here is the error output from the html (I was using my browser to hit the endpoint):
CreationException: Unable to create injector, see the following errors:
1) A binding to play.api.db.DBApi was already configured at play.api.db.DBModule.bindings(DBModule.scala:25):
Binding(interface play.api.db.DBApi to ProviderConstructionTarget(class play.api.db.DBApiProvider)) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1).
at play.api.db.slick.evolutions.EvolutionsModule.bindings(EvolutionsModule.scala:15):
Binding(interface play.api.db.DBApi to ConstructionTarget(class play.api.db.slick.evolutions.internal.DBApiAdapter) in interface javax.inject.Singleton) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
com.google.inject.CreationException: Unable to create injector, see the following errors:
com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:466)
com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:155)
com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107)
com.google.inject.Guice.createInjector(Guice.java:96)
com.google.inject.Guice.createInjector(Guice.java:84)
play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:181)
play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:123)
play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1$$anonfun$2.apply(DevServerStart.scala:168)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1$$anonfun$2.apply(DevServerStart.scala:164)
play.utils.Threads$.withContextClassLoader(Threads.scala:21)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:164)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:131)
scala.Option.map(Option.scala:146)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:131)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:129)
scala.util.Success.flatMap(Try.scala:231)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:129)
play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:121)
scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1423)
java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:902)
java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1689)
java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1644)
java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Here is my route (please note that I am not actually attempting to connect to the database yet):
GET /cities controllers.CityController.index
Which connects to this controller method:
def index = Action {
Ok(Json.obj("status" ->"OK", "message" -> "You did the thing!"))
}
And here is my application.conf, comments removed for brevity:
akka {
}
play.crypto.secret = "changeme"
play.modules {
}
play.i18n {
langs = [ "en" ]
}
play.http {
session {
}
flash {
}
}
play.server.netty {
}
play.ws {
ssl {
}
}
play.cache {
}
play.filters {
cors {
}
csrf {
}
headers {
}
hosts {
}
}
play.evolutions {
}
play.db {
prototype {
}
}
db {
db.default.driver=org.postgresql.Driver
db.default.jdbcUrl="jdbc:postgresql://localhost:5432/transit_service"
db.default.username="redacted"
db.default.password="redacted"
db.default.logSql=true
}
My evolution is a fairly simple create table statement that lives under conf/evolutions/default/1.sql.
I would appreciate any insight into why this error is occurring and how I can get the evolution to actually run.
As Matthias Eckhart pointed out in a comment: Removing the jdbc entry from libraryDependencies in build.sbt was the solution to the problem.
Related
I want to use JPA for micronaut. For that I am using io.micronaut.data:micronaut-data-hibernate-jpa:1.0.0.M1 library. Whenever I run my application and hit the endpoint to get the data, I get the following error:
{
message: "Internal Server Error: No backing RepositoryOperations configured for repository. Check your configuration and try again"
}
I tried looking up for errors but I couldn't find one. Attaching my files here. Please help.
build.gradle
plugins {
id "net.ltgt.apt-eclipse" version "0.21"
id "com.github.johnrengelman.shadow" version "5.0.0"
id "application"
}
version "0.1"
group "micronaut.test"
repositories {
mavenCentral()
maven { url "https://jcenter.bintray.com" }
}
configurations {
// for dependencies that are needed for development only
developmentOnly
}
dependencies {
annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
annotationProcessor "io.micronaut:micronaut-inject-java"
annotationProcessor "io.micronaut:micronaut-validation"
annotationProcessor "org.projectlombok:lombok:1.16.20"
annotationProcessor 'io.micronaut.data:micronaut-data-processor:1.0.0.M1'
implementation platform("io.micronaut:micronaut-bom:$micronautVersion")
compile 'io.micronaut.data:micronaut-data-hibernate-jpa:1.0.0.M1'
implementation "io.micronaut:micronaut-inject"
implementation "io.micronaut:micronaut-validation"
implementation "io.micronaut:micronaut-runtime"
implementation "io.micronaut:micronaut-http-server-netty"
implementation "io.micronaut:micronaut-http-client"
implementation 'nl.topicus:spanner-jdbc:1.1.5'
runtimeOnly "ch.qos.logback:logback-classic:1.2.3"
testAnnotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
testAnnotationProcessor "io.micronaut:micronaut-inject-java"
testImplementation "org.junit.jupiter:junit-jupiter-api"
testCompile "org.junit.jupiter:junit-jupiter-engine:5.1.0"
testImplementation "io.micronaut.test:micronaut-test-junit5"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
}
test.classpath += configurations.developmentOnly
mainClassName = "micronaut.test.Application"
// use JUnit 5 platform
test {
useJUnitPlatform()
}
tasks.withType(JavaCompile){
options.encoding = "UTF-8"
options.compilerArgs.add('-parameters')
}
shadowJar {
mergeServiceFiles()
}
run.classpath += configurations.developmentOnly
run.jvmArgs('-noverify', '-XX:TieredStopAtLevel=1', '-Dcom.sun.management.jmxremote')
Repository:
package micronaut.test.repo;
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
import micronaut.test.entity.Partner;
#Repository
public interface PartnerRepository extends CrudRepository<Partner,Integer> {
}
Service:
package micronaut.test.service;
import micronaut.test.entity.Partner;
import micronaut.test.repo.PartnerRepository;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;
#Singleton
public class SpannerService {
private PartnerRepository partnerRepository;
#Inject
public SpannerService(PartnerRepository partnerRepository) {
this.partnerRepository = partnerRepository;
}
public List<Partner> getPartners() {
return (List<Partner>) partnerRepository.findAll();
}
}
Controller:
package micronaut.test.controller;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
import micronaut.test.entity.Partner;
import micronaut.test.service.SpannerService;
import javax.inject.Inject;
import java.util.List;
#Controller("/micronaut")
public class MainController {
private SpannerService spannerService;
#Inject
public MainController(SpannerService spannerService) {
this.spannerService = spannerService;
}
#Get("/data")
#Produces(MediaType.APPLICATION_JSON)
public List<Partner> getPartners() {
return spannerService.getPartners();
}
}
stacktrace:
io.micronaut.context.exceptions.ConfigurationException: No backing RepositoryOperations configured for repository. Check your configuration and try again
at io.micronaut.data.intercept.DataIntroductionAdvice.findInterceptor(DataIntroductionAdvice.java:108)
at io.micronaut.data.intercept.DataIntroductionAdvice.intercept(DataIntroductionAdvice.java:76)
at io.micronaut.aop.MethodInterceptor.intercept(MethodInterceptor.java:40)
at io.micronaut.aop.chain.InterceptorChain.proceed(InterceptorChain.java:150)
at micronaut.test.repo.PartnerRepository$Intercepted.findAll(Unknown Source)
at micronaut.test.service.SpannerService.getPartners(SpannerService.java:22)
at micronaut.test.controller.MainController.getPartners(MainController.java:32)
at micronaut.test.controller.$MainControllerDefinition$$exec2.invokeInternal(Unknown Source)
at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:144)
at io.micronaut.context.DefaultBeanContext$BeanExecutionHandle.invoke(DefaultBeanContext.java:2792)
at io.micronaut.web.router.AbstractRouteMatch.execute(AbstractRouteMatch.java:235)
at io.micronaut.web.router.RouteMatch.execute(RouteMatch.java:122)
at io.micronaut.http.server.netty.RoutingInBoundHandler.lambda$buildResultEmitter$19(RoutingInBoundHandler.java:1408)
at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:71)
at io.reactivex.Flowable.subscribe(Flowable.java:14918)
at io.reactivex.Flowable.subscribe(Flowable.java:14865)
at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:68)
at io.reactivex.Flowable.subscribe(Flowable.java:14918)
at io.reactivex.internal.operators.flowable.FlowableMap.subscribeActual(FlowableMap.java:37)
at io.reactivex.Flowable.subscribe(Flowable.java:14918)
at io.reactivex.Flowable.subscribe(Flowable.java:14865)
at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:68)
at io.reactivex.Flowable.subscribe(Flowable.java:14918)
at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty.subscribeActual(FlowableSwitchIfEmpty.java:32)
at io.reactivex.Flowable.subscribe(Flowable.java:14918)
at io.reactivex.Flowable.subscribe(Flowable.java:14865)
at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:68)
at io.reactivex.Flowable.subscribe(Flowable.java:14918)
at io.reactivex.Flowable.subscribe(Flowable.java:14868)
at io.micronaut.http.context.ServerRequestTracingPublisher.lambda$subscribe$0(ServerRequestTracingPublisher.java:52)
at io.micronaut.http.context.ServerRequestContext.with(ServerRequestContext.java:52)
at io.micronaut.http.context.ServerRequestTracingPublisher.subscribe(ServerRequestTracingPublisher.java:52)
at io.reactivex.internal.operators.flowable.FlowableFromPublisher.subscribeActual(FlowableFromPublisher.java:29)
at io.reactivex.Flowable.subscribe(Flowable.java:14918)
at io.reactivex.Flowable.subscribe(Flowable.java:14865)
at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:68)
at io.reactivex.Flowable.subscribe(Flowable.java:14918)
at io.reactivex.Flowable.subscribe(Flowable.java:14865)
at io.reactivex.internal.operators.flowable.FlowableSubscribeOn$SubscribeOnSubscriber.run(FlowableSubscribeOn.java:82)
at io.reactivex.internal.schedulers.ExecutorScheduler$ExecutorWorker$BooleanRunnable.run(ExecutorScheduler.java:288)
at io.reactivex.internal.schedulers.ExecutorScheduler$ExecutorWorker.run(ExecutorScheduler.java:253)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [io.micronaut.data.operations.RepositoryOperations] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:1903)
at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:582)
at io.micronaut.data.intercept.DataIntroductionAdvice.findInterceptor(DataIntroductionAdvice.java:105)
... 43 common frames omitted
Micronaut currently supports only Tomcat JDBC, Apache DBCP2 and Hikari data sources providers out of the box (see https://micronaut-projects.github.io/micronaut-sql/latest/guide/#jdbc).
You can add this line into your build.gradle which adds Tomcat JDBC Data Source provider implementation into your project:
runtime "io.micronaut.configuration:micronaut-jdbc-tomcat"
Or you can choose another implementations like Apache DBCP2:
runtime "io.micronaut.configuration:micronaut-jdbc-dbcp"
Or Hikari:
runtime "io.micronaut.configuration:micronaut-jdbc-hikari"
For nl.topicus:spanner-jdbc data source provider you have to implement your own DatasourceFactory and DatasourceConfiguration for Micronaut because there is no one yet.
You can inspire your self in io.micronaut.configuration:micronaut-jdbc-tomcat. Sources are here: https://github.com/micronaut-projects/micronaut-sql/tree/master/jdbc-tomcat/src/main/java/io/micronaut/configuration/jdbc/tomcat
For example DatasourceFactory can then look like this:
#Factory
public class DatasourceFactory implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(DatasourceFactory.class);
private List<nl.topicus.jdbc.CloudSpannerDataSource> dataSources = new ArrayList<>(2);
private final DataSourceResolver dataSourceResolver;
/**
* Default constructor.
* #param dataSourceResolver The data source resolver
*/
public DatasourceFactory(#Nullable DataSourceResolver dataSourceResolver) {
this.dataSourceResolver = dataSourceResolver == null ? DataSourceResolver.DEFAULT : dataSourceResolver;
}
/**
* #param datasourceConfiguration A {#link DatasourceConfiguration}
* #return An Apache Tomcat {#link DataSource}
*/
#Context
#EachBean(DatasourceConfiguration.class)
public DataSource dataSource(DatasourceConfiguration datasourceConfiguration) {
nl.topicus.jdbc.CloudSpannerDataSource ds = new nl.topicus.jdbc.CloudSpannerDataSource();
ds.setJdbcUrl(datasourceConfiguration.getJdbcUrl());
...
dataSources.add(ds);
return ds;
}
#Override
#PreDestroy
public void close() {
for (nl.topicus.jdbc.CloudSpannerDataSource dataSource : dataSources) {
try {
dataSource.close();
} catch (Exception e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Error closing data source [" + dataSource + "]: " + e.getMessage(), e);
}
}
}
}
}
Anyway you can still use Google Cloud Spanner DB with Data Source providers like Hikari and Apache DBCP2. For example:
runtime 'nl.topicus:spanner-jdbc:1.1.5'
runtime "io.micronaut.configuration:micronaut-jdbc-hikari"
The first line adds JDBC driver and the second line adds Data Source provider which will use the spanner-jdbc JDBC driver.
It is known that the Google Cloud Spanner uses another dialect than other databases for Hibernate ORM. I think that this dialect is pretty new. Take a look over this repository 1. Maybe it will be useful for you, not necessary for solving your current issue, but giving you some other perspective.
I am trying to use Play Framework (Scala) as Akka Cluster client to send messages to another Akka Cluster running my app services.
Here is what I did:
I defined messages in different module using Protocol Buffer and shared between project running Services and Play app (using git submodules)
syntax = "proto2";
option java_package = "com.myproject.api.common.messages";
option java_outer_classname = "IsValidClientMessage";
message IsValidClient {
required int32 clientId = 1;
required string clientSecret = 2;
}
Started Services in port 2560
akka {
remote.netty.tcp.port=${?AKKA_REMOTE_PORT}
remote.netty.tcp.hostname=127.0.0.1
cluster {
seed-nodes = [
"akka.tcp://ApiServiceActorSystem#127.0.0.1:2560"
]
auto-down-unreachable-after = 10s
}
extensions = ["akka.cluster.client.ClusterClientReceptionist"]
loglevel = DEBUG
actor {
serializer {
proto = "akka.remote.serialization.ProtobufSerializer"
}
serialization-bindings {
"com.myproject.api.common.messages.IsValidClientMessage$IsValidClient" = proto
}
serialize-messages = on
provider = "akka.cluster.ClusterActorRefProvider"
debug {
receive = on
}
}
}
And ran Play App using below Akka config:
akka {
remote.netty.tcp.port=2552
remote.netty.tcp.hostname=127.0.0.1
remote.enabled-transports = ["akka.remote.netty.tcp"]
cluster {
seed-nodes = [
"akka.tcp://Api#127.0.0.1:2552"
]
auto-down-unreachable-after = 10s
}
extensions = ["akka.cluster.client.ClusterClientReceptionist"]
loglevel = DEBUG
actor {
serializer {
proto = "akka.remote.serialization.ProtobufSerializer"
}
serialization-bindings {
"com.myproject.api.common.messages.IsValidClientMessage$IsValidClient" = proto
}
serialize-messages = on
provider = "akka.cluster.ClusterActorRefProvider"
debug {
receive = on
}
}
}
This is the code I have been trying to send message to ApiServiceSystem:
package com.myproject.api.akka.actors.socket
import ...
class ClientActor extends Actor with ActorLogging {
ClusterClientReceptionist(context.system).registerService(self)
val outActors: ArrayBuffer[ActorRef] = ArrayBuffer.empty
val apiServiceClient = context.system.actorOf(ClusterClient.props(
ClusterClientSettings(context.system).withInitialContacts(Set(ActorPath.fromString("akka.tcp://ApiServiceActorSystem#127.0.0.1:2560/system/receptionist")))
))
override def receive = {
case WatchOutActor(a) =>
context.watch(a)
outActors += a
case Terminated(a) =>
context.unwatch(a)
outActors.remove(outActors.indexOf(a))
case other =>
implicit val to: Timeout = 2 seconds
val isValidClient = IsValidClient.newBuilder() // Protocol Buffer Message
isValidClient.setClientId(1000)
isValidClient.setClientSecret("notsosecret")
(apiServiceClient ? ClusterClient.Send("/user/clientActor", isValidClient.build(), false)).mapTo[Future[Either[ServiceError, Boolean]]] map { f =>
f map {
case Left(e) =>
outActors foreach { a => a ! e.msg }
case Right(bool) =>
outActors foreach { a => a ! bool.toString }
}
} recover {
case e: Exception => println(s"-=> Exception ${e.getMessage}")
}
}
}
object ClientActor {
case class WatchOutActor(actorRef: ActorRef)
}
As I see from below log, that my api has connected to cluster running service:
[DEBUG] [08/11/2016 10:11:05.936] [ApiServiceActorSystem-akka.remote.default-remote-dispatcher-24] [akka.remote.Remoting] Associated [akka.tcp://ApiServiceActorSystem#127.0.0.1:2560] <- [akka.tcp://Api#127.0.0.1:2552]
[DEBUG] [08/11/2016 10:11:05.998] [ApiServiceActorSystem-akka.remote.default-remote-dispatcher-24] [akka.serialization.Serialization(akka://ApiServiceActorSystem)] Using serializer[akka.cluster.client.protobuf.ClusterClientMessageSerializer] for message [akka.cluster.client.ClusterReceptionist$Internal$GetContacts$]
[DEBUG] [08/11/2016 10:11:06.000] [ApiServiceActorSystem-akka.actor.default-dispatcher-4] [akka.tcp://ApiServiceActorSystem#127.0.0.1:2560/system/receptionist] Client [akka.tcp://Api#127.0.0.1:2552/user/$a] gets contactPoints [akka.tcp://ApiServiceActorSystem#127.0.0.1:2560/system/receptionist] (all nodes)
[DEBUG] [08/11/2016 10:11:06.002] [ApiServiceActorSystem-akka.actor.default-dispatcher-4] [akka.tcp://ApiServiceActorSystem#127.0.0.1:2560/system/receptionist] Client [akka.tcp://Api#127.0.0.1:2552/user/$a] gets contactPoints [akka.tcp://ApiServiceActorSystem#127.0.0.1:2560/system/receptionist] (all nodes)
[DEBUG] [08/11/2016 10:11:06.002] [ApiServiceActorSystem-akka.actor.default-dispatcher-4] [akka.tcp://ApiServiceActorSystem#127.0.0.1:2560/system/receptionist] Client [akka.tcp://Api#127.0.0.1:2552/user/$a] gets contactPoints [akka.tcp://ApiServiceActorSystem#127.0.0.1:2560/system/receptionist] (all nodes)
[DEBUG] [08/11/2016 10:11:06.002] [ApiServiceActorSystem-akka.actor.default-dispatcher-4] [akka.tcp://ApiServiceActorSystem#127.0.0.1:2560/system/receptionist] Client [akka.tcp://Api#127.0.0.1:2552/user/$a] gets contactPoints [akka.tcp://ApiServiceActorSystem#127.0.0.1:2560/system/receptionist] (all nodes)
[DEBUG] [08/11/2016 10:11:06.004] [ApiServiceActorSystem-akka.remote.default-remote-dispatcher-76] [akka.serialization.Serialization(akka://ApiServiceActorSystem)] Using serializer[akka.cluster.client.protobuf.ClusterClientMessageSerializer] for message [akka.cluster.client.ClusterReceptionist$Internal$Contacts]
[DEBUG] [08/11/2016 10:11:06.033] [ApiServiceActorSystem-akka.remote.default-remote-dispatcher-76] [akka.serialization.Serialization(akka://ApiServiceActorSystem)] Using serializer[akka.serialization.JavaSerializer] for message [akka.actor.Identify]
[DEBUG] [08/11/2016 10:11:06.037] [ApiServiceActorSystem-akka.remote.default-remote-dispatcher-24] [akka.serialization.Serialization(akka://ApiServiceActorSystem)] Using serializer[akka.serialization.JavaSerializer] for message [akka.actor.ActorIdentity]
[DEBUG] [08/11/2016 10:11:06.126] [ApiServiceActorSystem-akka.remote.default-remote-dispatcher-76] [akka.serialization.Serialization(akka://ApiServiceActorSystem)] Using serializer[akka.serialization.JavaSerializer] for message [akka.remote.EndpointWriter$AckIdleCheckTimer$]
[DEBUG] [08/11/2016 10:11:07.749] [ApiServiceActorSystem-akka.remote.default-remote-dispatcher-76] [akka.serialization.Serialization(akka://ApiServiceActorSystem)] Using serializer[akka.cluster.client.protobuf.ClusterClientMessageSerializer] for message [akka.cluster.client.ClusterReceptionist$Internal$Heartbeat$]
But whenever I try to send message, I get this error:
java.lang.RuntimeException: Unable to find proto buffer class: com.myproject.api.common.messages.IsValidClientMessage$IsValidClient
at com.google.protobuf.GeneratedMessageLite$SerializedForm.readResolve(GeneratedMessageLite.java:1192)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at java.io.ObjectStreamClass.invokeReadResolve(ObjectStreamClass.java:1104)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1810)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at akka.serialization.JavaSerializer$$anonfun$1.apply(Serializer.scala:241)
at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
at akka.serialization.JavaSerializer.fromBinary(Serializer.scala:241)
at akka.serialization.Serialization$$anonfun$deserialize$3.apply(Serialization.scala:142)
at scala.util.Try$.apply(Try.scala:192)
at akka.serialization.Serialization.deserialize(Serialization.scala:142)
at akka.actor.dungeon.Dispatch$class.sendMessage(Dispatch.scala:128)
at akka.actor.ActorCell.sendMessage(ActorCell.scala:374)
at akka.actor.Cell$class.sendMessage(ActorCell.scala:295)
at akka.actor.ActorCell.sendMessage(ActorCell.scala:374)
at akka.actor.RepointableActorRef.$bang(RepointableActorRef.scala:169)
at akka.actor.ActorRef.tell(ActorRef.scala:128)
at akka.pattern.AskableActorRef$.internalAsk$extension(AskSupport.scala:295)
at akka.pattern.AskableActorRef$.$qmark$extension1(AskSupport.scala:281)
at com.myproject.api.akka.actors.socket.ClientActor$$anonfun$receive$1.applyOrElse(ClientActor.scala:43)
at akka.actor.Actor$class.aroundReceive(Actor.scala:480)
at com.myproject.api.akka.actors.socket.ClientActor.aroundReceive(ClientActor.scala:16)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
at akka.actor.ActorCell.invoke(ActorCell.scala:495)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
at akka.dispatch.Mailbox.run(Mailbox.scala:224)
at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
Caused by: java.lang.ClassNotFoundException: com.myproject.api.common.messages.IsValidClientMessage$IsValidClient
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.google.protobuf.GeneratedMessageLite$SerializedForm.readResolve(GeneratedMessageLite.java:1183)
... 38 common frames omitted
How can I serialize my message? Why is that ClassNotFoundException occurring at run time? Any help will be much appreciated
It seems there is a class loading issue with akka-remote and protobuf 3.x generated classes (even if you use proto2 definitions). Could solve it using the protobuf 2.5 compiler istead of 3.x: https://github.com/google/protobuf/releases/tag/v2.5.0
You have to compile it yourselve, because there are no precompiled binaries. Just head to the downloaded directory and run the following commands:
./configure
make
make install
After compiling the v2.5 protoc binary, use it to rebuild your java classes and use them. Will open an issue to get proto 3 support in akka.
Clarification: It's not the protobuf-java version, but the classes generated by the protoc binary 3.0
You need to explicitly bind Lite and V3 classes to your proto serializer:
akka {
loglevel = "INFO"
actor {
provider = "akka.remote.RemoteActorRefProvider"
serializers {
proto = "akka.remote.serialization.ProtobufSerializer" # or use your own..
}
serialization-bindings {
"com.google.protobuf.Message" = proto
"com.google.protobuf.GeneratedMessageLite" = proto
"com.google.protobuf.GeneratedMessageV3" = proto
}
}
I am trying to use DI to inject a webservice client in my application. I get runtime exception as i refer current application in the object being injected. Below is the skeletal of my code.
**import play.api.Play.Current**
#Singleton
class MicorServiceClient#Inject()(ws: WSclient) {
// References curret.configuraiton.getString(
....
}
class Application #Inject()(microServiceClient: MicorServiceClient) extends Controller {
def someMethod= Action {
//Calls the micorservicelient.getResponse()
}
}
I see following error when my api is called.
#709lj5bmd: Unexpected exception
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:170)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:126)
at scala.Option.map(Option.scala:146)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:126)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:124)
at scala.util.Success.flatMap(Try.scala:231)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:124)
at play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:116)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1689)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors:
1) Error injecting constructor, java.lang.RuntimeException: There is no started application
Ah, solved the issue, i need to pass play.api.Configuration to the Client and remove the references to current
so my MicroServiceClient definition looks like below
class MicroServiceClient(ws: WSclient, configuration: play.api.Configuration) {
}
I don't know how to configure GuiceApplicationBuilder in such a way, that I am able to load controllers that require a DatabaseConfigProvider to be injected.
I'd like to specify an alternative postgres database for testing, or an in memory database (if that is possible).
Code
class User
extends MySpecs
with OneAppPerTest
{
override def newAppForTest( testData: TestData ) = new GuiceApplicationBuilder()
// Somehow bind a database here, I guess?
.build()
"A test" should "test" in
{
val result = Application.instanceCache[api.controller.User]
.apply( app )
.list()( FakeRequest() )
...
}
}
Stacktrace
[info] - should return an entity *** FAILED ***
[info] com.google.inject.ConfigurationException: Guice configuration errors:
[info]
[info] 1) No implementation for play.api.db.slick.DatabaseConfigProvider was bound.
[info] while locating play.api.db.slick.DatabaseConfigProvider
[info] for parameter 1 at api.controller.User.<init>(User.scala:22)
[info] while locating api.controller.User
[info]
[info] 1 error
[info] at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1042)
[info] at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1001)
[info] at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051)
[info] at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:321)
[info] at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:316)
[info] at play.api.Application$$anonfun$instanceCache$1.apply(Application.scala:234)
[info] at play.api.Application$$anonfun$instanceCache$1.apply(Application.scala:234)
[info] at play.utils.InlineCache.fresh(InlineCache.scala:69)
[info] at play.utils.InlineCache.apply(InlineCache.scala:55)
[info] ...
You need to add a configuration to your GuiceApplicationBuilder(), then everything should be handled automatically by play framework. Something like this should help:
val app = new GuiceApplicationBuilder()
.configure(
Configuration.from(
Map(
"slick.dbs.YOURDBNAME.driver" -> "slick.driver.H2Driver$",
"slick.dbs.YOURDBNAME.db.driver" -> "org.h2.Driver",
"slick.dbs.YOURDBNAME.db.url" -> "jdbc:h2:mem:",
"slick.dbs.default.driver" -> "slick.driver.MySQLDriver$",
"slick.dbs.default.db.driver" -> "com.mysql.jdbc.Driver"
)
)
)
.in(Mode.Test)
.build()
There is a little bit of setting up in this approach but the final result is fair. First of all start with implementing your own GuiceApplicationLoader by extending it. See my answer how to implement it. Why your own application loader? You can specify different configs/modules per Prod/Dev/Test modes - as well as different data sources. Your main application.conf wouldn't have data source configured. Instead, you would move it to environment specific configurations which would be merged with main configuration by application loader anyway. Your dev.conf would look as follows:
slick.dbs {
default {
driver = "slick.driver.PostgresDriver$",
db {
driver = "org.postgresql.Driver",
url = "jdbc:postgresql://localhost:5432/dev",
user = "postgres"
password = "postgres"
}
}
}
And the trick now is to use same data source name, in this case default, for all other configurations (the database url, drivers, credentials etc. would be different). With such setup, your evolutions will be applied to your test and dev database. Your test.conf could look as follows:
slick.dbs {
default {
// in memory configuration
}
}
In your tests, use WithApplicationLoader with your custom application loader instead and thats it.
#RunWith(classOf[JUnitRunner])
class ApplicationSpec extends Specification {
"Application" should {
"return text/html ok for home" in new WithApplicationLoader(new CustomApplicationLoader) {
val home = route(FakeRequest(routes.ApplicationController.home())).get
status(home) must equalTo(OK)
contentType(home) must beSome.which(_ == "text/html")
}
}
}
Within the test itself, you have an access to app: Application value:
val service = app.injector.instanceOf(classOf[IService])
I am trying to use tminglei/slick-pg v9.0.0 with slick 3.0.0 and am getting an IllegalAccessException:
akka.actor.ActorInitializationException: exception during creation
at akka.actor.ActorInitializationException$.apply(Actor.scala:166) ~[akka-actor_2.11-2.3.11.jar:na]
...
Caused by: java.lang.RuntimeException: driverClassName specified class 'com.github.tminglei.MyPostgresDriver$' could not be loaded
at com.zaxxer.hikari.AbstractHikariConfig.setDriverClassName(AbstractHikariConfig.java:370) ~[HikariCP-java6-2.3.8.jar:na]
at slick.jdbc.HikariCPJdbcDataSource$$anonfun$forConfig$18.apply(JdbcDataSource.scala:145) ~[slick_2.11-3.0.0.jar:na]
at slick.jdbc.HikariCPJdbcDataSource$$anonfun$forConfig$18.apply(JdbcDataSource.scala:145) ~[slick_2.11-3.0.0.jar:na]
at scala.Option.map(Option.scala:146) ~[scala-library-2.11.7.jar:na]
at slick.jdbc.HikariCPJdbcDataSource$.forConfig(JdbcDataSource.scala:145) ~[slick_2.11-3.0.0.jar:na]
at slick.jdbc.HikariCPJdbcDataSource$.forConfig(JdbcDataSource.scala:135) ~[slick_2.11-3.0.0.jar:na]
at slick.jdbc.JdbcDataSource$.forConfig(JdbcDataSource.scala:35) ~[slick_2.11-3.0.0.jar:na]
at slick.jdbc.JdbcBackend$DatabaseFactoryDef$class.forConfig(JdbcBackend.scala:223) ~[slick_2.11-3.0.0.jar:na]
at slick.jdbc.JdbcBackend$$anon$3.forConfig(JdbcBackend.scala:33) ~[slick_2.11-3.0.0.jar:na]
...
Caused by: java.lang.IllegalAccessException: Class com.zaxxer.hikari.AbstractHikariConfig can not access a member of class com.github.tminglei.MyPostgresDriver$ with modifiers "private"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109) ~[na:1.7.0_79]
at java.lang.Class.newInstance(Class.java:373) ~[na:1.7.0_79]
at com.zaxxer.hikari.AbstractHikariConfig.setDriverClassName(AbstractHikariConfig.java:366) ~[HikariCP-java6-2.3.8.jar:na]
... 43 common frames omitted
HikariCP is the default connection pool in slick 3.0.0
I have defined the driver class much like in the example:
trait MyPostgresDriver extends ExPostgresDriver with PgArraySupport
with PgEnumSupport
with PgRangeSupport
with PgHStoreSupport
with PgSearchSupport{
override val api = new MyAPI {}
//////
trait MyAPI extends API
with ArrayImplicits
with RangeImplicits
with HStoreImplicits
with SearchImplicits
with SearchAssistants
}
object MyPostgresDriver extends MyPostgresDriver
My database config is pretty straightforward [excerpt of typesafe config follows]:
slick.dbs.default {
driver="com.github.tminglei.MyPostgresDriver$"
db {
driver="org.postgresql.Driver"
url="jdbc:postgresql://hostname:port/dbname"
user=user
password="pass"
}
}
It does not seem as if it should not work, and yet...
Should I change my driver class somehow? Is it something else?
Note: as can be seen in the stacktrace I am using
Java 1.7.0_79
Scala 2.11.7
akka 2.3.11 (I share the config instance for slick and akka)
slick 3.0.0
HikariCP-java6 2.3.8
tminglei's slick-pg_core 0.9.0
Lastly, when debugging thru the jdk code at Class.class (decompiled line 143)
Constructor tmpConstructor1 = this.cachedConstructor;
I get the following (toString'ed) value (as shown by intellij):
private com.github.tminglei.MyPostgresDriver$()
Could this be indicative of the problem? If so how should I fix it?
EDIT
I have replaced the custom driver configuration with the stock PostgresDriver like so:
slick.dbs.default {
driver="slick.driver.PostgresDriver$"
db {
driver="org.postgresql.Driver"
url="jdbc:postgresql://hostname:port/dbname"
user=user
password="pass"
}
}
The error is the same:
akka.actor.ActorInitializationException: exception during creation
...
Caused by: java.lang.RuntimeException: driverClassName specified class 'slick.driver.PostgresDriver$' could not be loaded
...
Caused by: java.lang.IllegalAccessException: Class com.zaxxer.hikari.AbstractHikariConfig can not access a member of class slick.driver.PostgresDriver$ with modifiers "private"
I had a similar problem.
I think you are using Database.forConfig("slick.dbs.default") but your config file is in DatabaseConfig format.
Instead, try using:
val dbConfig: DatabaseConfig[PostgresDriver] = DatabaseConfig.forConfig("slick.dbs.default")
val db = dbConfig.db