Creating API using scala Akka, code is running in IDE but when jar is created not able to run the code:
usind scala 2.11.12 and Below is build.sbt
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / scalaVersion := "2.11.12"
lazy val root = (project in file("."))
.settings(
name := "JNPM_SCALA_DATA_API",
// Ensure that the IDE-specific settings like 'idePackagePrefix' are necessary or correctly configured.
idePackagePrefix := Some("org.jio.jnpm"),
// Adding resolver within project settings
resolvers += "Typesafe Repo" at "https://repo.typesafe.com/typesafe/releases/",
// Library dependencies
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-http" % "10.0.15", // Updated to stable version
"com.typesafe.akka" %% "akka-actor" % "2.4.20",
"com.typesafe.akka" %% "akka-stream" % "2.4.20",
"com.typesafe.akka" %% "akka-http-spray-json" % "10.0.15" // JSON library for Akka HTTP
),
// Assembly settings
// assembly / assemblyMergeStrategy := {
// case PathList("META-INF", xs @ _*) => MergeStrategy.discard
// case "application.conf" => MergeStrategy.concat
// case _ => MergeStrategy.first
// }
// )
assembly / assemblyMergeStrategy := {
case PathList("META-INF", xs @ _*) => MergeStrategy.discard
case "application.conf" => MergeStrategy.concat
case _ => MergeStrategy.first
}
)
Below is application.conf:
akka {
loglevel = "INFO"
actor {
default-dispatcher {
type = "Dispatcher"
executor = "fork-join-executor"
fork-join-executor {
parallelism-min = 2
parallelism-factor = 2.0
parallelism-max = 10
}
}
}
stream {
materializer {
initial-input-buffer-size = 16
max-input-buffer-size = 128
}
}
}
Main method :
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.model.{HttpResponse, StatusCodes}
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Route
import models.{JsonSupport, ReportPayload}
import scala.concurrent.ExecutionContextExecutor
import akka.stream.ActorMaterializerSettings
import akka.http.scaladsl.server.RouteResult.route2HandlerFlow
import com.typesafe.config.ConfigFactory
object WebServer extends JsonSupport{
def main(args: Array[String]): Unit = {
implicit val system: ActorSystem = ActorSystem("akkaHttpServer", ConfigFactory.load())
println("Dispatcher Config: " + system.settings.config.getConfig("akka.actor.default-dispatcher"))
val materializerSettings = ActorMaterializerSettings(system).withDispatcher("akka.actor.default-dispatcher") //default-dispatcher
implicit val materializer: ActorMaterializer = ActorMaterializer(materializerSettings)(system)
implicit val executionContext: ExecutionContextExecutor = system.dispatcher
val route: Route = concat(
get {
path("hello") {
complete(HttpResponse(OK, entity = "Hello from Akka HTTP!"))
}
},
post {
path("echo") {
entity(as[String]) { body =>
complete(HttpResponse(OK, entity = body)) // Echoes back the posted content
}
}
},
post {
path("jnpm" / "pm" / "data" / "network_element") {
entity(as[ReportPayload]) { reportPayload =>
// Log received report details (simple console output for demonstration).
println(s"Received report for user: ${reportPayload.user_id}")
// Responds with a confirmation message.
complete(StatusCodes.OK, s"Report for ${reportPayload.report_name} received and is being processed.")
}
}
}
)
import akka.http.scaladsl.server.RouteResult._
val bindingFuture = Http().bindAndHandle(route, "0.0.0.0", 8088)
println(s"Server online at http://localhost:8080/nPress RETURN to stop...")
scala.io.StdIn.readLine() // Blocks here to keep the app running until the ENTER key is pressed.
bindingFuture
.flatMap(_.unbind()) // Disconnects the server from the port.
.onComplete(_ => system.terminate()) // Shuts down the actor system to clean up resources.
}
}
Getting below error :
Loading config from properties {java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=C:Program Files (x86)Javajre-1.8bin, java.vm.version=25.371-b11, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=;, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, user.script=, user.country=IN, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=, java.vm.specification.name=Java Virtual Machine Specification, user.dir=C:SCALA_SPARKJNPM_SCALA_DATA_APItargetscala-2.11, java.runtime.version=1.8.0_371-b11, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:Program Files (x86)Javajre-1.8libendorsed, os.arch=x86, java.io.tmpdir=C:UsersVIVEKA~1.DESAppDataLocalTemp, line.separator=
, java.vm.specification.vendor=Oracle Corporation, user.variant=, os.name=Windows 11, sun.jnu.encoding=Cp1252, java.library.path=C:Program Files (x86)Common FilesOracleJavajavapath;C:WindowsSunJavabin;C:Windowssystem32;C:Windows;C:Program Files (x86)Common FilesOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:WindowsSystem32OpenSSH;C:Program FilesWindowsPowerShellScripts;C:WindowsCCM;C:WindowsCCM;C:WindowsCCM;C:Program Files (x86)Enterprise VaultEVClientx64;C:WindowsCCM;C:WindowsCCM;C:WindowsCCM;C:UsersVivekanand.DesaiAppDataLocalProgramsPythonPython310Scripts;C:UsersVivekanand.DesaiAppDataLocalProgramsPythonPython310;C:UsersVivekanand.DesaiAppDataLocalProgramsPythonLauncher;C:UsersVivekanand.DesaiAppDataLocalMicrosoftWindowsApps;C:appshadoopbin;C:appsspark-3.5.0-bin-hadoop3spark-3.5.0-bin-hadoop3bin;C:appsspark-3.5.0-bin-hadoop3spark-3.5.0-bin-hadoop3sbin;C:Javajre-8u202-windows-x64jre1.8.0_202bin;;., java.specification.name=Java Platform API Specification, java.class.version=52.0, sun.management.compiler=HotSpot Client Compiler, os.version=10.0, user.home=C:UsersVivekanand.Desai, user.timezone=, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=Cp1252, java.specification.version=1.8, user.name=Vivekanand.Desai, java.class.path=JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar, java.vm.specification.version=1.8, sun.arch.data.model=32, java.home=C:Program Files (x86)Javajre-1.8, sun.java.command=JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar, java.specification.vendor=Oracle Corporation, user.language=en, awt.toolkit=sun.awt.windows.WToolkit, config.trace=loads, java.vm.info=mixed mode, java.version=1.8.0_371, java.ext.dirs=C:Program Files (x86)Javajre-1.8libext;C:WindowsSunJavalibext, sun.boot.class.path=C:Program Files (x86)Javajre-1.8libresources.jar;C:Program Files (x86)Javajre-1.8librt.jar;C:Program Files (x86)Javajre-1.8libjsse.jar;C:Program Files (x86)Javajre-1.8libjce.jar;C:Program Files (x86)Javajre-1.8libcharsets.jar;C:Program Files (x86)Javajre-1.8libjfr.jar;C:Program Files (x86)Javajre-1.8classes, sun.stderr.encoding=cp437, java.vendor=Oracle Corporation, java.specification.maintenance.version=4, file.separator=, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, sun.cpu.endian=little, sun.io.unicode.encoding=UnicodeLittle, sun.stdout.encoding=cp437, sun.desktop=windows, sun.cpu.isalist=}
Loading config from resource 'application.conf' URL jar:file:/C:/SCALA_SPARK/JNPM_SCALA_DATA_API/target/scala-2.11/JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar!/application.conf from class loader sun.misc.Launcher$AppClassLoader@647e05
Loading config from a URL: jar:file:/C:/SCALA_SPARK/JNPM_SCALA_DATA_API/target/scala-2.11/JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar!/application.conf
URL sets Content-Type: 'content/unknown'
'content/unknown' isn't a known content type
Loading config from class loader sun.misc.Launcher$AppClassLoader@647e05 but there were no resources called application.json
exception loading application.json: java.io.IOException: resource not found on classpath: application.json
Loading config from class loader sun.misc.Launcher$AppClassLoader@647e05 but there were no resources called application.properties
exception loading application.properties: java.io.IOException: resource not found on classpath: application.properties
Loading config from resource 'reference.conf' URL jar:file:/C:/SCALA_SPARK/JNPM_SCALA_DATA_API/target/scala-2.11/JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar!/reference.conf from class loader sun.misc.Launcher$AppClassLoader@647e05
Loading config from a URL: jar:file:/C:/SCALA_SPARK/JNPM_SCALA_DATA_API/target/scala-2.11/JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar!/reference.conf
URL sets Content-Type: 'content/unknown'
'content/unknown' isn't a known content type
Looking for 'version.conf' relative to ParseableResourceURL(jar:file:/C:/SCALA_SPARK/JNPM_SCALA_DATA_API/target/scala-2.11/JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar!/reference.conf)
Looking for 'version.json' relative to ParseableResourceURL(jar:file:/C:/SCALA_SPARK/JNPM_SCALA_DATA_API/target/scala-2.11/JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar!/reference.conf)
Looking for 'version.properties' relative to ParseableResourceURL(jar:file:/C:/SCALA_SPARK/JNPM_SCALA_DATA_API/target/scala-2.11/JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar!/reference.conf)
Loading config from resource 'version.conf' URL jar:file:/C:/SCALA_SPARK/JNPM_SCALA_DATA_API/target/scala-2.11/JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar!/version.conf from class loader sun.misc.Launcher$AppClassLoader@647e05
Loading config from a URL: jar:file:/C:/SCALA_SPARK/JNPM_SCALA_DATA_API/target/scala-2.11/JNPM_SCALA_DATA_API-assembly-0.1.0-SNAPSHOT.jar!/version.conf
URL sets Content-Type: 'content/unknown'
'content/unknown' isn't a known content type
Loading config from class loader sun.misc.Launcher$AppClassLoader@647e05 but there were no resources called version.json
exception loading version.json: java.io.IOException: resource not found on classpath: version.json
Loading config from class loader sun.misc.Launcher$AppClassLoader@647e05 but there were no resources called version.properties
exception loading version.properties: java.io.IOException: resource not found on classpath: version.properties
Loading config from a String resizer.enabled=on
Dispatcher Config: Config(SimpleConfigObject({"executor":"fork-join-executor","fork-join-executor":{"parallelism-factor":2,"parallelism-max":10,"parallelism-min":2},"type":"Dispatcher"}))
Exception in thread "main" com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'dispatcher'
at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(SimpleConfig.java:152)
at com.typesafe.config.impl.SimpleConfig.findOrNull(SimpleConfig.java:170)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:184)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:189)
at com.typesafe.config.impl.SimpleConfig.getString(SimpleConfig.java:246)
at akka.stream.ActorMaterializerSettings$.apply(ActorMaterializer.scala:267)
at akka.stream.ActorMaterializerSettings$.apply(ActorMaterializer.scala:258)
at org.jio.jnpm.WebServer$.main(WebServer.scala:31)
at org.jio.jnpm.WebServer.main(WebServer.scala)
Code run fine in IDE but not using jar file. It is looking for keyword dispatcher not sure where its is looking