spring boot cmd line cron expression java.lang.IllegalStateException - spring-boot

I do have an argument with my Spring Boot powered web application. I'm trying to override a #Scheduled cron expression from the command line, but spring responds with a java.lang.IllegalStateException.
Initialization of bean failed;
nested exception is java.lang.IllegalStateException:
Encountered invalid #Scheduled method 'work':
Cron expression must consist of 6 fields (found 1 in "0")
I have a Spring Component with a Scheduled Annotation:
#Scheduled(cron="${myapp.cron}")
public void work() {
...
}
There is an application.properties file like this:
myapp.cron=0 0 1 * * *
I'm bundling the application with maven to a jar file. The application runs on an ubuntu machine with Java 8 as an init task (/etc/init/myapp.conf).
description "My app"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 10 5
setuid <USER>
setgid <GROUP>
script
java -Xms2G -Xmx2G -jar /opt/myapp.jar \
--server.port=4014 \
--server.address=127.0.0.1
--logging.file=/opt/myapp.log \
--logging.level.root=INFO
end script
Until this point everything is fine. Adding the following line gives the above mention exception.
--myapp.cron=0 0 8 * * *
Any ideas? What's wrong?
Cheers,
Kai

Kai,
You're passing in the 0 0 8 * * * as command line parameter in order to get it in the Spring environment. However, your app only gets the first 0 as variable value, hence the exception about the required 6 parts. Surround the value with quotes instead:
--myapp.cron="0 0 8 * * *"

create a xyz.properties file under src/main/resouce folder.
cron.open.status.mgr.schedule=10 15 0 15 * ?
and do something like below
#Component
public class OpenStatusManagerScheduler {
#Scheduled(cron = "${open.status.mgr.schedule}")
public void scheduleStatusTaskWithCronExpression() {}
}

Related

pyspark: org.xml.sax.SAXParseException Current config of the parser doesn't allow a maxOccurs attribute value to be set greater than the value 5,000

I am trying to parse xml files with XSD using spark-xml library in pyspark.
Below is the code :
xml_df = spark.read.format("com.databricks.spark.xml") \
.option("rootTag", "Document") \
.option("rowTag", "row01") \
.option("rowValidationXSDPath","auth.011.001.02_ABC_1.1.0.xsd") \
.load("/mnt/bronze/ABC-3.xml")
I am getting error as
org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 0.0 failed 4 times, most recent failure: Lost task 0.3 in stage 0.0 (TID 3) (10.0.1.4 executor driver): java.util.concurrent.ExecutionException: org.xml.sax.SAXParseException; systemId: file:/local_disk0/auth.011.001.02_ABC_1.1.0.xsd; lineNumber: 5846; columnNumber: 99; Current configuration of the parser doesn't allow a maxOccurs attribute value to be set greater than the value 5,000.
I have looked for the ways to setup jdk.xml.maxOccurLimit=0 in databricks cluster but didn't find any.
Any help on solving this error will be highly appreciated.
As per documentation, you can setup jdk.xml.maxOccurLimit=0 and also follow below sample code:
I reproduce same in my environment got this output:
Sample Code:
spark.conf.set("spark.jvm.args", "-Djdk.xml.maxOccurLimit=0")
df = spark.read.format("com.databricks.spark.xml").option("rowTag", "book").load("dbfs:/FileStore/gg.xml")
display(df)

Springboot chaos-monkey assaults [killApplicationActive and memoryActive] not working

I am using Springboot 2.3.1.RELEASE and chaos monkey its working fine for latencyActive and exceptionsActive.
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>chaos-monkey-spring-boot</artifactId>
<version>2.2.0</version>
</dependency>
Following two assaults not working
Kill application
chaos.monkey.assaults.killApplicationActive=true
chaos.monkey.assaults.level=3
Memory
chaos.monkey.assaults.memoryActive=true
chaos.monkey.assaults.memoryMillisecondsHoldFilledMemory=90000
chaos.monkey.assaults.memoryMillisecondsWaitNextIncrease=1000
chaos.monkey.assaults.memoryFillIncrementFraction=90.15
chaos.monkey.assaults.memoryFillTargetFraction=90.25
App kill and memory kill attacks need the attribute runtimeAssaultCronExpression set to a valid cron expression like " * * * * * * ". By default it is set to "OFF"
See documentation: https://codecentric.github.io/chaos-monkey-spring-boot/2.2.0/#_appkiller_assault
To Kill an application manually follow the below step:
1) Create the assault by:
POST: https://{{server-address}}/{{app-name}}/actuator/chaosmonkey/assaults
{
"level": 1,
"deterministic": false,
"latencyActive": false,
"exceptionsActive": false,
"killApplicationActive": true,
"memoryActive": false,
"cpuActive": false }
2) Run the created assault by:
POST: https://{{server-address}}/{{app-name}}/actuator/chaosmonkey/assaults/runtime/attack
Use property value for "chaos.monkey.assaults.runtime.scope.assault.cron.expression" as cron expression like */1 * * * * ? or any valid cron expression, to enable chaos monkey runtime assaults on a schedule.
Use property value as OFF otherwise(also default value)
links:
https://codecentric.github.io/chaos-monkey-spring-boot/2.1.0/#configuration
https://www.programmersought.com/article/11861551911/
LatencyActive and ExceptionsActive assaults are low impact assault(Request type), whereas KillApp and Memory are high impact assaults(Runtime assault). So Request type assault can be triggered just by loading them whereas Runtime assault requires an additional step after loading them.
Trigger this Http endpoint and that should execute the assault:
/chaosmonkey/assaults/runtime/attack

Groovy-based Spring Boot task choking on configured cron

Not sure if this is purely a Spring Boot issue, purely a Groovy issue, or a problem arising from using Groovy to build a Spring Boot app. I have a Spring Boot background task that -- in production -- I want running once an hour:
#Component
class MyTask {
#Scheduled(cron = "${tasks.mytask.cron}")
void doSomething() {
// blah whatever
}
}
In my application.yml file I have:
logging:
config: 'logback.groovy'
server:
port: 9200
error:
whitelabel:
enabled: false
spring:
cache:
type: none
myapp:
detailsMode: ${detailsMode:Terse}
verification: 5
tasks:
mytask:
cron: '0 0/1 * 1/1 * ? *'
However for local development I want to be able to change the cron expression (for testing, etc.). When I go to compile this I get:
Expected '$tasks.mytask.cron' to be an inline constant of type java.lang.String in #org.springframework.scheduling.annotation.Scheduled
# line 31, column 23.
#Scheduled(cron = "${tasks.mytask.cron}")
Any ideas what I need to do to fix this? I need an externally-configurable value like tasks.mytask.cron that I can define in my app properties/YAML.
myapp:
detailsMode: ${detailsMode:Terse}
verification: 5
tasks:
mytask:
cron: '0 0/1 * 1/1 * ?'
or
#Scheduled(cron = '${myapp.tasks.mytask.cron}')
also notice that your cron format is incorrect

Cron expression must consist of 6 fields (found 1 in "#{systemEnvironment['db_cron']}")

I'm trying to set a cron Scheduled annotation as follows:
#Scheduled(cron = "#{systemEnvironment['db_cron']}")
def void schedule() {
}
Next set the environment variable as:
export db_cron="0 19 21 * * *"
However, I get the runtime error:
Cron expression must consist of 6 fields
(found 1 in "#{systemEnvironment['db_cron']}")
What can be going wrong?
EDIT
I have also tried: #Scheduled(cron = "${db_cron}")
But this returns a compile time error:
/Users/snowch/repos/sales/Prospects/Snow/Plugwise/etl_service/src/main/groovy/com/ibm/etl/Application.groovy: 52: Expected '$db_cron' to be an inline constant of type java.lang.String in #org.springframework.scheduling.annotation.Scheduled
# line 52, column 23.
#Scheduled(cron = "${db_cron}")
^
/Users/snowch/repos/sales/Prospects/Snow/Plugwise/etl_service/src/main/groovy/com/ibm/etl/Application.groovy: -1: Attribute 'cron' should have type 'java.lang.String'; but found type 'java.lang.Object' in #org.springframework.scheduling.annotation.Scheduled
# line -1, column -1.
2 errors
:compileGroovy FAILED
FAILURE: Build failed with an exception.
I have tried #Scheduled(cron = "#{db_cron}")
But the runtime error was:
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: static org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.setSystemPropertiesMode() is applicable for argument types: (java.lang.Integer) values: [2]
Possible solutions: setSystemPropertiesMode(int), setSystemPropertiesModeName(java.lang.String)
at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1373)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1359)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:50)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at com.ibm.etl.Application.main(Application.groovy:43)
Note the environment variable is set as follows:
snowch$ echo "$db_cron"
0 19 21 * * *
You should set env variable like you do:
export db_cron="0 19 21 * * *"
then restart your ide if you are using or restart your terminal session.
#Scheduled(cron = "${db_cron}")
def void schedule() {
...
}
I tried it and here is my screenshot. Everything works as expected...
For me worked using JDK 11 and Spring 2.3.3 adding double quotes both the variables:
export db_cron="0 19 21 * * *"
and:
#Scheduled(cron = "${db_cron}")
Note: works in application.properties files too.

How to call Nashorn from Java

I have a batfile that I want to call from java. I made a nashorn script, that runs successfully from commandline and from Netbeans 'run' command. Here it is:
#!C:/Program Files/Java/jdk1.8.0_25/bin -fv
$ENV.PWD = 'C:/Hydromodel/MapGenerate';
$EXEC("cmd /c start cmd_all.bat");
print ('finished!');
I try to call the script from java this way:
public class RunCmd {
public void nashornTest() {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
try {
Object eval = engine.eval("load('src/simple/a_test.js');");
//Object eval = engine.eval(new FileReader("C:/_Udvikling/Maven/realtid_2014/src/main/java/simple/a_test.js"));
} catch (Exception ex ) {
Logger.getLogger(CmdTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Both ways I get this error:
Severe: javax.script.ScriptException: TypeError: Cannot load script from src/simple/a_test.js in <eval> at line number 1
Even if the javascript file is reduced to:
print ('hello!');
I also tried this SO proposed way
The Error stays.
I googled, but most people seem to use Linux or Mac when writing about Nashorn.
Should I do anything special to call Nashorn from java on a Windows box?
mbwiese
I am running Netbeans 8 and JDK 8 with Glassfish 4.1 on Windows 7

Resources