Sunday, September 22, 2019

Loading external jar for SpringBoot

In case one builds one Springboot app which requires jar to be provided on runtime, here's the recipe:

  1. Change "Main-Class" attribute in jar manifest, here's one example of configuration in gradle:
    bootJar {
        manifest {
            attributes(
                "Main-Class": "org.springframework.boot.loader.PropertiesLauncher"
            }
        }
  2. To run the app, add loader.path parameter pointing to the folder location of the jar
    java -Dloader.path=lib/ -jar application.jar
Tips:
  1.  Default "Main-Class" in Springboot jar
    "Main-Class": "org.springframework.boot.loader.PropertiesLauncher
  2. "Start-Class" attribute in the manifest points to the main class, the class annotated with SpringBootApplication

Tuesday, March 12, 2019

Spring Boot Quartz - Custom Scheduler Factory using QuartzProperties

To have Quartz scheduler in a spring boot project we can follow the steps here.
The example on the link above requires separate properties file for quartz (or manually injecting the property values, see here).

To do it in a cleaner way, quartz configuration should be placed together in the application.yml file. Follow the steps below to adjust the configuration bean:
1. Inject QuartzProperties
@Autowired
QuartzProperties quartzProperties

2. Use the quartz properties on your schedulerFactory
schedulerFactory.setQuartzProperties(asProperties(quartzProperties.getProperties()))

private Properties asProperties(Map source) {
    Properties properties = new Properties();
    properties.putAll(source);
    return properties;
}

3. Done!

Sample quartz configuration in application.yml file:
quartz:
  job-store-type: jdbc
  jdbc:
    initialize-schema: embedded
  overwrite-existing-jobs: true
  scheduler-name: TheScheduler
  properties:
    org:
      quartz:
        scheduler:
          instanceName: TheScheduler
          instanceId: AUTO
        threadPool:
          class: org.quartz.simpl.SimpleThreadPool
          threadCount: 10
          threadPriority: 5
        jobStore:
          misfireThreshold: 60000
          class: org.quartz.impl.jdbcjobstore.JobStoreTX
          driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
          useProperties: false
          tablePrefix: QRTZ_
          isClustered: true
          clusterCheckinInterval: 20000


Reference:

  • https://www.baeldung.com/spring-quartz-schedule
  • https://dzone.com/articles/integrating-quartz-withspring


Wednesday, January 23, 2019

OpenVPN Server Setup

If you ever need to setup a vpn either for securing your communication (read: internet privacy) or providing remote access to your server, OpenVPN might be one good try.

Just finished working on OpenVPN setup on Ubuntu, here's the how-to link:
https://www.digitalocean.com/community/tutorials/how-to-set-up-an-openvpn-server-on-ubuntu-16-04

Several notes (all the commands below are executed as root):

1. Configuration file

/etc/openvpn/server.conf

2. Some useful commands
Check status: systemctl status openvpn@server 
Start: systemctl start openvpn@server 
Stop: systemctl stop openvpn@server 
Starts automatically: systemctl enable openvpn@server 
Check OpenVPN tun0 interface up: ip addr show tun0

3. Allow client to client connection
- Edit server.conf enable client-to-client
# Uncomment this directive to allow different
# clients to be able to "see" each other.
# By default, clients will only see the server.
# To force clients to only see the server, you
# will also need to appropriately firewall the
# server's TUN/TAP interface.
client-to-client
- Restart openvpn service

4. Adding new client
cd ~/openvpn-ca
source vars
./build-key-pass client1
(change client1 to any client name)
Note:
the defaults should be populated, so you can just hit ENTER to continue. Leave the challenge password blank and make sure to enter y for the prompts that ask whether to sign and commit the certificate.
5. Generate client config
cd ~/client-configs
./make_config.sh client1
(client1 should be replace with the client name used to generate the key)
File will be generated in ~/client-configs/files/client1.ovpn
Transfer the ovpn file to client
6. Client Setup
For Mac, tunnelblick works for me (https://tunnelblick.net/).
For Windows, OpenVPN provides one (https://openvpn.net/community-downloads/)