Spring Groovy Integration - java

I am a newbie to Spring and Groovy integration.
I have some strange requirement. Please help in achieving it.
I have Groovy scripts in a database and in a flow I want to get a script from the database and execute the script.
But the problem here is I want the Spring container inside that script and I want to call any beans of Spring container from Groovy script.
Now the problem is how to specify the Spring container to scan the Groovy scripts in database. Also Groovy scripts get added and removed from the database. I also don't want to restart the application when a new script is added.

Related

Dynamodb best practices to setup the database

I have a Java microservice and I would like to use Dynamodb as a database.
I have multiple profiles: dev, test, stage and prod.
I would like to use for internal profiles (dev and test) a local database using the docker-local image.
My idea is to automate the setup process for test profile (because I'll use Jenkins in order to run the tests for CI/CD) as also for dev profile in order to avoid to run a lot of commands manually (I would like to run just a setup script in order to create the db, the tables and the indexes also).
I saw that, after the initialization of the database using the docker compose, it's possible to use the aws cli in order to create the tables, indexes, etc
The idea is to run these commands into a bash script in order to setup everything.
I also saw that there is this project https://github.com/derjust/spring-data-dynamodb
The idea is to reuse the Hibernate idea in order to write just the entities java class having the automatic creation/update of the database tables/indexes.
In particular I don't like this approach because this is not an 'official' project.. just this.
About the production environment the idea is to integrate this configuration step into the Cloudformation script.
Could this be a good way or is it better to configure all using the AWS console?
Honestly this is the first time that I'm using DynamoDB and I'm not a clear idea about the best practices.
Can you point me to some good examples?

How can I specify an external configuration file for a java application that is not spring boot

Background
I have built a console java application using kotlin and gradle.
The gradle file creates a fat jar which I can run from the command line using
java -jar <project>.jar
The jar contains the application.properties file from which properties are read.
Problem
I would like to specify on the command line that the application.properties file should be read from some external path.
When using spring boot, I have used
java -jar -Dspring.config.location=somepath/application.properties <project>.jar
and this works.
But it does not seem to be working in the non-spring boot application
Question
Is it possible to specify external configuration on the command line for non spring boot applications?
Spring boot has a whole chapter in the documentation which deals with various ways of configuration.
Obviously if you don't have spring boot you should implement something similar to it by yourself.
First thing you should decide - at which level you need the configuration to be integrated into your application:
Do you only want to read the key/values from command line or maybe rely on environment variables or system properties?
In general, what is the source of your configuration: Yaml? Properties file? maybe consul or etc.d?
Do you want to create a java object that reflects the configurations that you've read (like classes annotated with #ConfigurationProperties in spring boot do?
Do you want to support only one source of configuration or you want the various sources of configurations to be supported?
If you ware using Spring, do you want configuration properties to be automatically injected into beans?
If you're planning to use properties/yaml (like application.properties in spring boot) - where do you want to place them? Non spring boot application won't read them "auto-magically", you'll have to implement this logic.
Are you planning to deal with profiles (non-spring-boot application still supports flavors of loading different beans depending on specified profile).
Spring boot has answered all these questions and more.
Here are some options that you might want to give a try to if you're running outside the spring boot context but still have spring application:
Since spring 3.1, I guess, there is a#PropertySource annotation that you can use to make spring load properties from the file in the classpath or some "place" in the filesystem. This article summarizes the usage of this method as well as compares what spring boot has up on its sleeves as opposed to regular spring application. This is also a nice tutorial that covers regular spring features.
Something out of spring eco-system but still can be useful: apache common configuration project. There are some workarounds to integrate it with spring application, see here
Considering all the answers here I concluded that though it is possible to enable external configuration in applications that are not spring boot, it does require some effort.
Therefore I decided to use Spring Boot in the container.

What are the pros/cons of using the Gradle integration vs Spring Boot integration for Flyway?

Flyway has several integration options.
I'm trying to determine what the pros/cons are of using the Gradle integration vs the Spring Boot integration given that your project is already using both Spring Boot and Gradle.
The only thing I can think of is that if you want to be able to do migrations without starting the application or want to save time by not migrating every time you start the app then the Gradle choice could be better.
Think of it as build time vs run time.
In general you will build an artifact once and deploy it to many environments, so run time is a much better fit.
However sometimes build time makes sense. This is primarily for situations where you need a fully migrated database as part of the build, in order to for example generate code based on the structure of that database using frameworks like jOOQ or QueryDSL.

Pre-populate MongoDB database with Maven

I want to pre-populate MongoDB database in Maven Java project with some data for integration testing purposes. I can easily achieve this by executing script in mongo shell manually (./mongo server:port/database --quiet my_script.js), but how can I do this using Maven?
Is the MongoDB database running before the tests are run? Or do you start the server as part of your tests?
In either case, I think it would be possible to use dbunit maven plugin to load an external file with testdata. Take a look at http://mojo.codehaus.org/dbunit-maven-plugin/operation-mojo.html
You could use the Mongo Java API to run your script into the database with the DB.doEval() method. If you already have a running database then connecting and running the script could be done as part of your test setup.
That said, for some of my projects, I am using Maven with embedmongo Maven plugin, which allows a mongo database to be created on the fly for integration testing. That, combined with your script might be an alternate solution that does not rely on an existing Mongo database to be running.
You can now use embedmongo-maven-plugin to achieve this. It has a mongoimport goal to run mongoimport or a mongo-scripts goal to eval arbitrary js scripts.

Running a simple Spring batch job without using maven?

I am new to Spring batch framework. Can we create a batch job and run that without using maven?
I have seen spring batch samples connecting spring batch with maven..
Can anyone give me a sample Spring batch project that can run without using maven dependencies? Or at least give an idea about this.
Yes, run them in a container like tomcat see here for more information; Spring-Batch-standalone-Server-architecture.

Categories

Resources