Welcome to Techy Tunes !

Follow Me

What is Cron ?


  • Cron is a unix based job scheduler tool. 
  • Can be configured to execute tasks at a specified time.
  • We can use our java programs to make use of the Cron tool by configuring cron expressions. 
  • Here we focus on writing cron expressions which is compatible with the Spring's CronTrigger. 
  • Configuring these cron expressions with Spring is coming soon !!!

Cron Expression Format


Field NameMandatoryAllowed ValuesAllowed Special Characters
SecondsYES0-59, - * /
MinutesYES0-59, - * /
HoursYES0-23, - * /
Day of monthYES1/31/2015, - * ? / L W
MonthYES1-12 or JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC, - * /
Day of weekYES1-7 or SUN, MON, TUE, WED, THU, FRI, SAT, - * ? / L #
YearNOempty, 1970-2099, - * /

Here's an old school trick to help you to memorize this ;) If this is too dumb for you, make your own one :D :D

Sexy          Minions,    Our      DOMbAsses,    Mouthed   Dow's              Yearbook  :D :D :D

Seconds    Minutes     Hours   Day Of Month  Month      Day of Week    Year

  • Anyway, Spring's CronTrigger accept only 6 fields starting with Seconds, up to Day of Week.
  • So, a cron format could be "5/10 10-30 10 ? * MON,TUE". Once caught, this is really easy.

Special Characters


  • * - all values. In the above example ,the star corresponds to every month.
  • - - a range of values. In the above example, 0-15 means, it should start in 10th minute and end in 30th minute.
  • x/y - starts at value x. Run again when another y number of time units passed. In the above example, the cron starts at the 5th second, and thereafter run when each 10 seconds passed. means, it runs in 15, 25, 35, 45, 55 th seconds. 
  • , - specifies individual values. In the above example, MON,TUE means, this cron runs in Monday and Tuesday only
  • With above four characters we can write something like this "0-5,10,15,20/10  * * ? * SAT,MON,SUN". The section "0-5,10,15,20/10 " means, the cron runs from 0th seconds to 5th second, then at 10th and 15th seconds, then each 10 seconds after starting from 20th second. 
  • ? - Allowed only in Day of Month and Day of Week fields. There's a small catch here. In above example;
    • Say, we put 15 to the Day of Month's position and the value MON to the Day of Week's position. Then it is a huge ambiguity. 
    • Putting 15 implies that the cron will be run in every 15th day of the month. Putting WED implies that cron will be run on every Monday of the week. 
    • So,  will this cron fire on 15th of the month if it falls on a Monday, Or should it fire on both the 15th day of the month and every Monday. (drunk)
    • Here's the solution. You cannot put two values there. That's where "?" comes to play
    • If you specify the Day of month, you put "?" to the Day of week and vice versa. That's it , no more questions. ;)

Executing Cron Jobs with Java


  • To make java programs to execute these cron jobs we use Spring's org.springframework.scheduling.support.CronTrigger and it seems to support for Vixie Cron, where the special characters L, W and # are not supported. 
  • We can use org.springframework.scheduling.quartz.CronTriggerBean to support the above mentioned characters, but that is not our focus here.
  • How to do this ? I will post next and will update here. 

<3 With Love <3



Prism is a really cool, simple and lightweight syntax highlighter. Prism supports bunch of languages and 6 different themes by today. First, see how prism looks.

public class Main {
    public static void main(String[] args) {
        System.out.println("This is how prism looks");
    }
}

Download Prism JS

  • Hop into prismjs official website to download the required stuff
  • Go to Download page
  • You can download the development or minified version. Since we are going to use it straight away, we need the minified version
  • Select a theme from Themes section. You can see a live preview at the bottom of the page
  • In the Languages section , select all the languages you need to be included in your syntax highlighter
  • There are bunch of Plugins, select whatever you need, as per your requirement.
  • Finally, use DOWNLOAD CSS and DOWNLOAD JS buttons to download both .css and .js files.

Host Prism JS

  • Now we have to add these js and css files to blogger template. To do this, blogger needs to read these files through a cdn (Content Delivery Network). 
  • Simply, CDN makes available web content to the users, in our case its our js and css files. The downloaded files should be available online, where blogger can read them from the hosted location.
  • We can use dropbox to host our js and css files and its free !!!
  • Create a dropbox account and upload prism files to the Public folder. I have created a separate folder named CDN, and uploaded the two prism files
  • A default CDN for prism js is available here, but here you cannot get the advantage of the customizations provided by prism. That is the reason of hosting your own prism in dropbox

Add Prism JS to Blogger

  • Now we are ready to add Prism to blogger. Go to your blog's Template section. Click on the Backup / Restore button to download the full working template. By doing this, you can restore the template if something goes wrong.
  • Now click on Edit Html button. This will take you to the blogger template editor. 
  • Locate the </head> tag and add following lines immediate before the </head> tag

<link href='https://dl.dropboxusercontent.com/u/55229039/CDN/prism_custom.css' rel='stylesheet' type='text/css'/>

<script src='https://dl.dropboxusercontent.com/u/55229039/CDN/prism_custom.js' type='text/javascript'/>

  • These two links should get from the files we just uploaded to dropbox . 
  • Go to your file hosted location in dropbox and click on each of the file and click "Copy public link…" option to get their public link. Add them to your <link> and <script> tags.

  • Now save the template and preview it to be sure everything works fine and same as earlier
  • If everything goes well, we are done with adding prism to Blogger. Now we simply have to use it in our code.

Using Prism JS to highlight syntax

  • Cannot think of a simpler way to highlight syntax. Its just the below code

<pre><code class="language-xxx">### Your code here ###</code></pre>

  • The "xxx" is language-xxx should be replaced with the relevant language alias. If it is css

<pre><code class="language-css">p { color: red }</code></pre>

  • When you are pasting the code make sure it is encoded properly. If you paste the code to the blog post editor directly when it is in the "Compose" mode, it will encode the code automatically. Or else you can use a tool like this to encode your code.

Prism JS language aliases

  • I have mentioned below, all the language aliases available as per today. You can find them here too. 
  • But, keep in mind, to get the language support, you should select that specific language when you are downloading prism libraries.
  • Now, we are done. Make your blog a beauty with Prism !!! I would like to hear from you.

  • Markup - markup
  • CSS - css
  • C-like - clike
  • JavaScript - javascript
  • ActionScript - actionscript
  • Apache Configuration - apacheconf
  • AppleScript - applescript
  • ASP.NET (C#) - aspnet
  • AutoHotkey - autohotkey
  • Bash - bash
  • C - c
  • C# - csharp
  • C++ - cpp
  • CoffeeScript - coffeescript
  • CSS Extras - css-extras
  • Dart - dart
  • Eiffel - eiffel
  • Erlang - erlang
  • F# - fsharp
  • Fortran - fortran
  • Gherkin - gherkin
  • Git - git
  • Go - go
  • Groovy - groovy
  • Haml - haml
  • Handlebars - handlebars
  • Haskell - haskell
  • HTTP - http
  • Ini - ini
  • Jade - jade
  • Java - java
  • Julia - julia
  • LaTeX - latex
  • Less - less
  • LOLCODE - lolcode
  • Markdown - markdown
  • MATLAB - matlab
  • NASM - nasm
  • NSIS - nsis
  • Objective-C - objectivec
  • Pascal - pascal
  • Perl - perl
  • PHP - php
  • PHP Extras - php-extras
  • PowerShell - powershell
  • Python - python
  • R - r
  • React JSX - jsx
  • reST (reStructuredText) - rest
  • Rip - rip
  • Ruby - ruby
  • Rust - rust
  • SAS - sas
  • Sass (Scss) - scss
  • Scala - scala
  • Scheme - scheme
  • Smalltalk - smalltalk
  • Smarty - smarty
  • SQL - sql
  • Stylus - stylus
  • Swift - swift
  • Twig - twig
  • TypeScript - typescript
  • Wiki markup - wiki
  • YAML - yaml
There are couple of other ways to copy resources. but I find this is the easiest method.

maven-resources-plugin

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
    <execution>
        <id>copy-libs</id>
        <phase>process-resources</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>${basedir}/target/my-project/WEB-INF/lib</outputDirectory>
            <resources>
                <resource>
                    <directory>${deps.home}/lib</directory>
                </resource>
            </resources>
        </configuration>
    </execution>
    <execution>
        <id>copy-xml</id>
        <phase>process-resources</phase>
        <goals>
            <goal>copy-resources</goal>
        </goals>
        <configuration>
            <outputDirectory>${basedir}/target/classes/com/blogspot/techytunes/my-project/dao</outputDirectory>
            <resources>
                <resource>
                    <directory>${basedir}/src/main/java/com/blogspot/techytunes/my-project/dao/</directory>
                    <excludes>
                        <exclude>*.java</exclude>
                    </excludes>
                </resource>
            </resources>
        </configuration>
    </execution>
</executions>
</plugin>

  • We need multiple executions ( We need to copy from different sources to different destinations) . That's why we need the <executions> tag. Inside this tag we can define several executions using <execution> tag.
  • First, I have to copy some libs. I give an id to that process as <id>copy-libs</id>
  • Maven build life cycle has several phases. We need to use "process-resources" phase with the goal "copy-resources"
  • When packaging the web-app, maven takes the project folder from the target directory and creates the war file. In my case, it takes the my-project folder and packages its content to a war file. This happens in the package phase.
  • We add the basic configuration in the <configuration> tag. <outputDirectory> specifies the destination to copy.
  • In the <resources> tag we can add multiple <resource> elements. Everything we put in here, will be copied to the above destination.
  • To copy to another destination, we simply need another execution.
  • The second execution copy-xml is slightly different from copying resources. Some xml files needed to be copied to the destination of classes. 
  • After the process-resources phase there comes compile phase where maven compile all the classes to the target/classes directory. So we take the required xml files and copy them to the relevant destination in the target/classes/... 
  • we pick the xml files from src/main/java/com/blogspot/techytunes/my-project/dao and copy them to target/classes/com/blogspot/techytunes/my-project/dao
  • In the source directory (src/main/java/com/blogspot/techytunes/my-project/dao) we have .java files along with the xml. We use <excludes> tag to exclude .java files being copied to the destination.
  • When maven is in his compile phase, he will compile the classes and put it to the target/classes directory
Thats it. Good or bad, comments are always welcome :D
I had developed a simple json api. There I used
When I do a curl as "curl http://my-domain/my-app/url" it worked fine and produced me desired results.

Then I developed the front-end of the app and hosted it in a separate server. Now, my api and the client app are hosted in two different servers. I used jQuery to access the api and get the required data from it. But when I do it , this is all I got :(

No 'Access-Control-Allow-Origin' header is present on the requested resource
 Same as you, I googled :D found out that this happens because of the same origin policy. In a nutshell, same origin policy prevents JavaScript from making requests across domains. In my case , the api and the app were in two separate domains and there you have it :)

To avoid this add this lines to <CATALINA_HOME>/conf/web.xml

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

We do this because we only use Apache2 to redirect request purpose.

Restart Tomcat then you are good to go !!!