How do I reference a config file in Scala WorkSheet and Akka? - scala

I'm trying to create a simple akka system in Scala Worksheet but each time I try, I get this error.
com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'akka'
at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(tsets.sc:148)
at com.typesafe.config.impl.SimpleConfig.findKey(tsets.sc:141)
at com.typesafe.config.impl.SimpleConfig.findOrNull(tsets.sc:168)
at com.typesafe.config.impl.SimpleConfig.find(tsets.sc:180)
at com.typesafe.config.impl.SimpleConfig.find(tsets.sc:185)
at com.typesafe.config.impl.SimpleConfig.getString(tsets.sc:242)
at akka.actor.ActorSystem$Settings.<init>(tsets.sc:166)
at akka.actor.ActorSystemImpl.<init>(tsets.sc:533)
at akka.actor.ActorSystem$.apply(tsets.sc:139)
at akka.actor.ActorSystem$.apply(tsets.sc:116)
at #worksheet#.system$lzycompute(tsets.sc:9)
at #worksheet#.system(tsets.sc:9)
at #worksheet#.get$$instance$$system(tsets.sc:9)
at A$A9$.main(tsets.sc:35)
at #worksheet#.#worksheet#(tsets.sc)
Even though the Scala worksheet is in the exact same directory as the application.conf file.
The akka entry for the file looks something like this.
akka {
loglevel = ??
actor {
provider = ??
}
remote {
log-remote-lifecycle-events = ??
enabled-transports =??
netty.tcp {
hostname = ??
port = ??
maximum-frame-size = ??
maximum-frame-size = ??
send-buffer-size = ??
send-buffer-size = ??
receive-buffer-size = ??
receive-buffer-size = ??
}
}
cluster {
roles = ["frontend"]
seed-nodes = ??
use-dispatcher = c??
failure-detector {
threshold = ??
acceptable-heartbeat-pause = ??
heartbeat-interval = ??
heartbeat-request {
expected-response-after = ??
}
}
}
}
I have even tried adding this to the build.sbt
assemblyMergeStrategy in assembly := {
case PathList("application.conf") => MergeStrategy.concat
}

One of the possible variants is calling ConfigFactory.parseFile with resolve (if you have some substitutions like receive-buffer-size = ${send-buffer-size} in your config file):
val confPath = getClass.getResource("/application.conf")
val config = ConfigFactory.parseFile(new File(confPath.getPath)).resolve()
config.getString("akka.loglevel")
Your application.conf file should be in resources folder and your worksheet can be placed in scala folder.
Update
If you use Intellij IDEA. There is a workaround for this
Go to File > Settings > Languages & Frameworks > Scala > Worksheet
Untick "Run config in the compiler process" and press "OK"
So you can use ConfigFactory.load() freely
I used IDEA with version 2018.1.5

Related

SSLHandshakeException happens during file upload to AWS S3 via Alpakka

I'm trying to setup an Alpakka S3 for files upload purpose. Here is my configs:
alpakka s3 dependency:
...
"com.lightbend.akka" %% "akka-stream-alpakka-s3" % "0.20"
...
Here is application.conf:
akka.stream.alpakka.s3 {
buffer = "memory"
proxy {
host = ""
port = 8000
secure = true
}
aws {
credentials {
provider = default
}
}
path-style-access = false
list-bucket-api-version = 2
}
File upload code example:
private val awsCredentials = new BasicAWSCredentials("my_key", "my_secret_key")
private val awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials)
private val regionProvider = new AwsRegionProvider { def getRegion: String = "us-east-1" }
private val settings = new S3Settings(MemoryBufferType, None, awsCredentialsProvider, regionProvider, false, None, ListBucketVersion2)
private val s3Client = new S3Client(settings)(system, materializer)
val fileSource = Source.fromFuture(ByteString("ololo blabla bla"))
val fileName = UUID.randomUUID().toString
val s3Sink: Sink[ByteString, Future[MultipartUploadResult]] = s3Client.multipartUpload("my_basket", fileName)
fileSource.runWith(s3Sink)
.map {
result => println(s"${result.location}")
} recover {
case ex: Exception => println(s"$ex")
}
When I run this code I get:
javax.net.ssl.SSLHandshakeException: General SSLEngine problem
What can be a reason?
The certificate problem arises for bucket names containing dots.
You may switch to
akka.stream.alpakka.s3.path-style-access = true to get rid of this.
We're considering making it the default: https://github.com/akka/alpakka/issues/1152

playframework slick deprecation warning concerning driver and profile

I got this message:
[warn] s.b.DatabaseConfig - Use `profile` instead of `driver`. The latter is deprecated since Slick 3.2 and will be removed.
my config looks like this:
slick.dbs.default.driver = "slick.driver.MySQLDriver$"
slick.dbs.default.db.driver = "com.mysql.jdbc.Driver"
slick.dbs.default.db.url = "jdbc:mysql://"
slick.dbs.default.db.user = ""
slick.dbs.default.db.password = ""
how do I have to change this to remove the warn?
thanks
Try the following:
slick.dbs.default.profile = "slick.jdbc.MySQLProfile$"
slick.dbs.default.db.driver = "com.mysql.jdbc.Driver"
slick.dbs.default.db.url = "jdbc:mysql://"
slick.dbs.default.db.user = ""
slick.dbs.default.db.password = ""
The same thing in the HOCON format:
slick {
dbs {
default {
profile = "slick.jdbc.MySQLProfile$"
db {
driver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://"
user = ""
password = ""
}
}
}
}

Scala no configuration key found

I could extract value for
val ranking_score_path = cf.getString(stg + ".input.path.ranking_score")
.replaceAll("_replace_date_", this_date)
and
val output_path = cf.getString(stg + ".output.path.hdfs") + tomz_date + "/"
but not
val AS_HOST = cf.getString(stg + ".output.path.aerospike.host")
println("AS_HOST = " + AS_HOST)
I have tried
replacing . with _,
adding commas
but didnt work.
error log
Exception in thread "main" com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'production.output.path.aerospike'
at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(SimpleConfig.java:152)
at ...
application.conf
production {
input {
path {
local = "/home/aduser/tmp/"
hdfs = "/user/aduser/tmp_vincent/CPA/_replace_date_/intermediate/l1/"
ranking_score = "/home/aduser/plt/item_performance/pipeline/cpa/output/_replace_date_/predict_output/ranking_score.csv"
}
}
output {
path {
local = "/home/aduser/tmp/"
hdfs = "/user/aduser/dyson/display/"
aerospike {
host = "0.0.0.0"
port = 3000
namespace = "test"
set = "spark-test2"
}
}
}
}
Reply # Comment #1
the cf is very long but the important part is as follows
... ore.csv"}},"output":{"path":{"hdfs":"/user/aduser/dyson/display/","local":"/home/aduser/tmp/"}}},"sun":{"arch": ...
Effort #1: Replaced part of the application.conf
path {
local = "/home/aduser/tmp/"
hdfs = "/user/aduser/dyson/display/"
ae_host = "0.0.0.0"
ae_port = 3000
ae_namespace = "test"
ae_set = "spark-test2"
}
and changed the calling method
val AS_HOST = cf.getString(stg + ".output.path.ae_host")
println("AS_HOST = " + AS_HOST)
but still getting errors
Exception in thread "main" com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'production.output.path.ae_host'
at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(SimpleConfig.java:152)
I have found the problem after several hours, my application.conf was at the same level with src.main.scala and it worked partially for reasons which I don't know. It worked perfectly after creating src.main.resources and putting application.conf inside.

com.typesafe.config.ConfigException$NotResolved: has not been resolved,

I am trying to read the following config file using typesafe config
common = {
jdbcDriver = "com.mysql.jdbc.Driver"
slickDriver = "slick.driver.MySQLDriver"
port = 3306
db = "foo"
user = "bar"
password = "baz"
}
source = ${common} {server = "remoteserver"}
target = ${common} {server = "localserver"}
When I try to read my config using this code
val conf = ConfigFactory.parseFile(new File("src/main/resources/application.conf"))
val username = conf.getString("source.user")
I get an error
com.typesafe.config.ConfigException$NotResolved: source.user has not been resolved, you need to call Config#resolve(), see API docs for Config#resolve()
I don't get any error if I put everything inside "source" or "target" tags. I get errors only when I try to use "common"
I solved it myself.
ConfigFactory.parseFile(new File("src/main/resources/application.conf")).resolve()
I solved it.
Config confSwitchEnv = ConfigFactory.load("env.conf");
the env.conf is in the resources dir.
reference: https://nicedoc.io/lightbend/config

How to set Gradle Artifactory Publish plugin default configuration/properties from custom plugin

I have been trying to set the properties of publish(PublisherConfig), defaults(defaultsClosure) from my custom plugin. What is the best way to do this ?
I tried the following;
Approach 1: Tried setting the properties on the extensions
project.getExtensions().publishing.getProperties().each { println it }
Approach 2: Tried adding compile time dependency on the Gradle Artifactory plugin and importing the plugin classes...
```
if (project.plugins.hasPlugin("com.jfrog.artifactory")) {
println "I found jfrog.artifactory plugin"
Plugin jfrogArtifactory = project.getPlugins().getPlugin("com.jfrog.artifactory")
ArtifactoryPluginConvention apc = new ArtifactoryPluginConvention(project);
project.getExtensions().add("artifactory", apc);
apc.contextUrl = 'https://myrepo.com/artifactory/'
PublisherConfig pc = new PublisherConfig(apc);
pc.defaults {
println "in my plugin pc.defaults : " + it.metaClass
publications('mavenJava')
publishConfigs('archives', 'published')
properties = ['my.gitCommitUrl': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitCommitUrl, 'my.gitHash': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitHash, 'my.gitBranch': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitBranch]
publishBuildInfo = true //Publish build-info to Artifactory (true by default)
publishArtifacts = true //Publish artifacts to Artifactory (true by default)
publishPom = true //Publish generated POM files to Artifactory (true by default).
publishIvy = false //Publish generated Ivy descriptor files to Artifactory (true by default).
}
pc.repository {
println "in my plugin pc.repository : " + it.metaClass
repoKey = 'my-mvn' //The Artifactory repository key to publish to
username = project.findProperty("artifactory_user") ?: "" //The publisher user name
password = project.findProperty("artifactory_api_key") ?: "" //The publisher password
}
apc.setPublisherConfig(pc)
}
```
Compile and Build are successful, artifact and its info is not published.
:artifactoryPublish
BUILD SUCCESSFUL
Total time: 3.313 secs
Here is what worked for me!!!
`
if (project.plugins.hasPlugin("com.jfrog.artifactory")) {
Plugin jfrogArtifactory = project.getPlugins().getPlugin("com.jfrog.artifactory")
//maven install task is required to publishPom (using artifactory plugin)
if (!project.plugins.hasPlugin("maven")) {
project.apply(plugin: MavenPlugin)
}
Closure artifactoryPublishClosure = {
contextUrl = 'https://myrepo.com/artifactory/'
publications('mavenJava')
publishConfigs('archives', 'published')
properties = ['gitCommitUrl': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitCommitUrl, 'gitHash': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitHash, 'gitBranch': project.getExtensions().findByType(BuildPropertiesPluginExtension.class).gitBranch]
publishBuildInfo = true //Publish build-info to Artifactory (true by default)
publishArtifacts = true //Publish artifacts to Artifactory (true by default)
publishPom = true //Publish generated POM files to Artifactory (true by default).
publishIvy = false //Publish generated Ivy descriptor files to Artifactory (true by default).
// Redefine basic properties of the build info object
clientConfig.setIncludeEnvVars(true)
clientConfig.timeout = 600 // Artifactory connection timeout (in seconds). The default timeout is 300 seconds.
publish {
//A closure defining publishing information
repository {
repoKey = 'my-mvn' //The Artifactory repository key to publish to
username = project.findProperty("artifactory_user") ?: "" //The publisher user name
password = project.findProperty("artifactory_api_key") ?: "" //The publisher password
}
}
resolve {
repository {
repoKey = 'my-mvn' //The Artifactory (preferably virtual) repository key to resolve from
}
}
}
project.getTasks().findByName("artifactoryPublish").configure(artifactoryPublishClosure)
}
`