Create scheduled task in Spring Boot

Reference: https://spring.io/guides/gs/scheduling-tasks/

pom.xml:

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>

<groupId>com.sample</groupId>
<artifactId>gs-scheduling-tasks</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

ScheduledTasks.java

package com.sample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

/**
* Created by vagrant on 5/25/17.
*/
@Component
public class ScheduledTasks {

private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

private static final SimpleDateFormat dateFormat = new SimpleDateFormat(“HH:mm:ss”);

@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
log.info(“The time is now {}”, dateFormat.format(new Date()));
}
}

App.java

package com.sample;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
* Hello world!
*
*/
@SpringBootApplication
@EnableScheduling
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class);
}
}

To run: mvn spring-boot:run

a script to install basic Java dev tools for Vagrant provision

#! /bin/bash
echo install jdk1.8
wget –no-cookies –no-check-certificate –header “Cookie: oraclelicense=accept-securebackup-cookie” “http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm&#8221;
yum localinstall jdk-8u131-linux-x64.rpm -y
touch /etc/profile.d/java.sh
cat <<EOT >> /etc/profile.d/java.sh
export JAVA_HOME=”/usr/java/jdk1.8.0_131″
export JAVA_PATH=”$JAVA_HOME”
export PATH=”$PATH:$JAVA_HOME”
EOT
rm jdk-8u131-linux-x64.rpm

echo install ant
wget http://www.us.apache.org/dist/ant/binaries/apache-ant-1.10.1-bin.tar.gz
tar xvfz apache-ant-1.10.1-bin.tar.gz -C /opt
ln -s /opt/apache-ant-1.10.1 /opt/ant
sh -c ‘echo ANT_HOME=/opt/ant>>/etc/environment’
ln -s /opt/ant/bin/ant /usr/bin/ant
rm apache-ant-1.10.1-bin.tar.gz

echo install python3.6
yum -y groupinstall development
yum -y install https://centos7.iuscommunity.org/ius-release.rpm
yum -y install python36u python36u-pip python36u-devel
# yum -y install python36u-pip
# yum -y install python36u-devel

echo install Ansible
yum -y install ansible

echo set basic vim style
touch /etc/vimrc
cat <<EOT >> /etc/vimrc
filetype plugin indent on
set tabstop=2
set shiftwidth=2
set expandtab
set softtabstop=2
set nowrap
set number
color blue
EOT

echo install maven
wget http://apache.mirror.gtcomm.net/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.tar.gz
tar xvf apache-maven-3.5.0-bin.tar.gz -C /usr/local
rm apache-maven-3.5.0-bin.tar.gz
ln -s /usr/local/apache-maven-3.5.0/ /usr/local/maven
touch /etc/profile.d/maven.sh
cat <<EOT >> /etc/profile.d/maven.sh
#! /bin/bash

export M2_HOME=/usr/local/maven
export PATH=$PATH:/usr/local/maven/bin
EOT

su -l vagrant -c “mkdir /home/vagrant/.m2”
touch /home/vagrant/.m2/settings.xml
cat <<EOT >> /home/vagrant/.m2/settings.xml
<settings>
<proxies>
<proxy>
<active>true</active>
<protocol>http</protocol>
<host>localhost</host>
<port>3128</port>
<nonProxyHosts>127.0.0.1</nonProxyHosts>
</proxy>
</proxies>
</settings>
EOT

chown vagrant /home/vagrant/.m2/settings.xml

echo install SDKMAN
yum install -y zip unzip
su -l vagrant -c ‘curl -s “https://get.sdkman.io&#8221; | bash’
su -l vagrant -c ‘source /home/vagrant/.sdkman/bin/sdkman-init.sh’

echo install Spring Boot
su -l vagrant -c ‘sdk install springboot’

echo install Intellij
sudo yum install -y xauth libXtst
wget http://download.jetbrains.com/idea/ideaIC-2017.1.3.tar.gz
tar xvf ideaIC-2017.1.3.tar.gz
mv idea-IC-171.4424.56 /usr/lib
ln -sf /usr/lib/idea-IC-171.4424.56 /usr/lib/idea
ln -sf /usr/lib/idea/bin/idea.sh /usr/bin/idea
sed -i ‘s/Xm\(s\|x\)[[:digit:]]*m/Xmx\12048m/g’ /usr/lib/idea/bin/idea.vmoptions /usr/lib/idea/bin/idea64.vmoptions
sed -i ‘s/ReservedCodeCacheSize=[[:digit:]]*m/ReservedCodeCacheSize=700m/g’ /usr/lib/idea/bin/idea.vmoptions /usr/lib/idea/bin/idea64.vmoptions
sed -i ‘4i -XX:MaxPermSize=512m’ /usr/lib/idea/bin/idea.vmoptions /usr/lib/idea/bin/idea64.vmoptions
cat <<EOT >> /etc
sysctl -p

Sample java application to test using headless selenium server

 

public class App 
{
    private static void googleExampleThatSearchesFor(final String searchString) throws MalformedURLException {
        WebDriver driver =
                new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.chrome());
        driver.get("http://www.google.com");
        WebElement searchField = driver.findElement(By.name("q"));
        searchField.sendKeys(searchString);
        System.out.println("Page title is: " + driver.getTitle());
        searchField.submit();

        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driverObject) {
                return driverObject.getTitle().toLowerCase().startsWith(searchString.toLowerCase());
            }
        });

        System.out.println("Page title is: " + driver.getTitle());

        driver.quit();
    }
    public static void main( String[] args ) throws Exception{
        googleExampleThatSearchesFor("Cheese!");
    }
}

How to install JDK8 or JDK9 on Ubuntu

from: https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-16-04

  • sudo add-apt-repository ppa:webupd8team/java
  • sudo apt-get update
  • sudo apt-get install oracle-java8-installer
  • sudo update-alternatives –config java

 

From: https://github.com/franzwong/til/blob/master/java/silent-install-oracle-jdk8-ubuntu.md

Silent install Oracle JDK 8 on Ubuntu

Problem: During installation of Oracle JDK 8 on Ubuntu, we need to agree on license agreement manually.

Solution: Installing Java Automatically (With Silent Option)

Shell script:

sudo apt-get install -y python-software-properties debconf-utils
sudo add-apt-repository -y ppa:webupd8team/java
sudo apt-get update
echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | sudo debconf-set-selections
sudo apt-get install -y oracle-java8-installer

Ansible:

---
- name: Add apt repository
  apt_repository:
      repo: 'ppa:webupd8team/java'
      state: present
      update_cache: yes

- name: Accept license
  debconf:
    name: 'oracle-java8-installer'
    question: 'shared/accepted-oracle-license-v1-1'
    value: 'true'
    vtype: 'select'

- name: Install java
  apt: name={{ item }} state=present
  with_items:
      - oracle-java8-installer
      - ca-certificates
      - oracle-java8-set-default

One to One mapping in hibernate

<hibernate-mapping package=”gar.entity”>

<class name=”RTPRequest” table=”rtp_request”>
<id name=”id” type=”long”>
<column name=”id” />
<generator class=”sequence”>
<param name=”sequence”>seq_rtp_request</param>
<param name=”allocationSize”>1</param>
<param name=”initialValue”>1</param>
</generator>
</id>

<one-to-one name=”garnishment” class=”Garnishment” cascade=”all”></one-to-one>

</class>

</hibernate-mapping>

 

===================================================

<hibernate-mapping package=”gar.entity”>

<class name=”Garnishment” table=”garnishment”>

<id name=”id” type=”long”>
<column name=”id”/>
<generator class=”foreign”>
<param name=”property”>rtpRequest</param>
</generator>
</id>

<one-to-one name=”rtpRequest” class=”RTPRequest” constrained=”true”></one-to-one>

</class>

</hibernate-mapping>

set default jdk using alternatives

https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Web_Platform/5/html/Installation_Guide/sect-use_alternatives_to_set_default_JDK.html

 

⁠C.4.  Setting the default JDK with the /usr/sbin/alternatives Utility

/usr/sbin/alternatives is a tool for managing different software packages that provide the same functionality.Red Hat Enterprise Linux uses /usr/sbin/alternatives to ensure that only one Java Development Kit is set as the system default at one time.

Important

Installing a Java Development Kit from the Red Hat Network will normally result in an automatically configured system. However, if multiple JDKs are installed, it is possible that /usr/sbin/alternativesmay contain conflicting configurations. Refer to Procedure C.4, “ Using /usr/sbin/alternatives to Set the Default JDK ” for syntax of the /usr/sbin/alternatives command.

Procedure C.4.  Using /usr/sbin/alternatives to Set the Default JDK

  1. Become the root user.

    /usr/sbin/alternatives needs to be run with root privileges. Use the su command or other mechanism to gain these privileges.
  2. Set java.

    Input this command: /usr/sbin/alternatives --config java
    Next, follow the on-screen directions to ensure that the correct version of java is selected. Table C.1, “javaalternative commands” shows the relevant command settings for each of the different JDKs.

    Table C.1. java alternative commands

    JDK alternative command
    OpenJDK 1.6 /usr/lib/jvm/jre-1.6.0-openjdk/bin/java
    Sun Microsystems JDK 1.6 /usr/lib/jvm/jre-1.6.0-sun/bin/java
  3. Set javac.

    Enter this command: /usr/sbin/alternatives --config javac
    Follow the on-screen directions to ensure that the correct version of javac is selected. Table C.2, “javacalternative commands” shows the appropriate command settings for the different JDKs.

    Table C.2. javac alternative commands

    JDK alternative command
    OpenJDK 1.6 /usr/lib/jvm/java-1.6.0-openjdk/bin/javac
    Sun Microsystems JDK 1.6 /usr/lib/jvm/java-1.6.0-sun/bin/javac
  4. Extra Step: Set java_sdk_1.6.0.

    The Sun Microsystems JDK 1.6 requires an additional command be run:
    /usr/sbin/alternatives --config java_sdk_1.6.0
    Follow the on-screen directions to ensure that the correct java_sdk is selected. It is /usr/lib/jvm/java-1.6.0-sun.

example to use keytool to set up public/private keys

  1. Run the following two commands to generate keystore
    keytool –genkeypair –alias dlkey -keypass password -storetype jks –keystore dlkeystore.jks -storepass password -validity 365 -keyalg RSA
  2. Export certificate of private key dlkey
    keytool –exportcert –alias dlkey -file dlkey.cert -keystore dlkeystore.jks -storepass password
  3. Send the dlkey.cert (public key) to target server.
  4. Run the following command to import the server certificate (fnet.net.cer) to keystore

keytool –importcert –file fnet.net.cer -alias dlcert -keypass password -keystore dlkeystore.jks –storepass password