How to run 2 timers on the same TimerTask at different delays in Java.

Lets say you want to run a process on a timer at a random time between 0-5 minutes, but then once that process is complete, you want to run the same process again after 5 minutes?  I have a easy solution for you to try out. All you will use is the java.util.Timer and TimerTask libraries.

1. Create a class that extends TimerTask, in this case I will create TestTimerTask.java.

2. Add the following code. We are creating a Timer property that we will pass into the constructor. Then we create a startTimer method that will purge the prior task, then schedule a new task in 5 minutes (300000 ms) with a new instance of TestTimerTask and Timer.  Once run is complete, startTimer is called, and will execute the same run method in approximately 5 minutes.


import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class TestTimerTask extends TimerTask {

    Timer timer;

    public TestTimerTask(Timer timer){
        this.timer = timer;
    }

    public void startTimer(){
        this.timer.purge();
        timer = new Timer();
        timer.schedule(new TestTimerTask(timer), 300);
    }

    @Override
    public void run() {
        try{
            Thread.sleep(500);
        } catch (Exception e) {
            e.printStackTrace();
        }
        startTimer();
    }
}

3. In your main method, create a Timer instance and TestTimerTask instance and pass in a new timer. Then schedule a task with a random time between 0-5 mins (0-600000ms).
This is a quick and easy solution that will help you solve the problem above. I tried other solutions but could not accomplish my scenario. If anyone has a cleaner solution please let me know in the comments below!

import java.util.Random;
import java.util.Timer;

public class Main {

    public static void main(String[] args) {
        Timer timer = new Timer();
        TestTimerTask task = new TestTimerTask(timer);
        timer.schedule(task, new Random().nextInt(600000));
    }
}

How to add DNS to heroku app with GoDaddy.

Prerequisites

  1. Heroku App already deployed. ex url: something.herokuapp.com
  2. GoDaddy domain
Steps

1. In GoDaddy, select the domain you want to configure.
2. Under Domain Settings, Scroll down and select Manage DNS.


3. Under Records you will see CNAME, edit that CNAME and replace @ with your heroku domain under value. If CNAME is not there, add a new CNAME record.

4. Go to Heroku and go to your app settings. Scroll down to Domains and Certificates. Add your GoDaddy domain twice, once with www and once without. 

5. Go to your domain and you site should appear, if not, try clearing your cache and cookies. 

How to highlight text in a Rich Text Editor (QuillJS) and display on screen.

I recently participated in a hack-a-thon and worked on a annotation tool. The front end purpose of the tool was to highlight entities such as “Organizations”, “Locations” and “People”. I used a free rich text editor called Quill JS with KnockoutJS to easily highlight text with your curser and display on the screen above. This is only the tip of the iceberg as Quill is so powerful and contains many different api calls that can be used. You can use any front-end UI framework you prefer, but in this case I will use Knockout JS.

See Codepen for live example.
1. Include the cdn for KnockoutJS and QuillJS. Create the editor container and include the js file you will be loading from. I set an inline style for max-height so it won’t take up the entire page.
 <head>
        <link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
 </head>
 <body>
      <div id="editor" style="max-height: 500px">
        <p>Hello World! My name is George, this is text that can be highlighted.</p>
        <p><br></p>
      </div>
      
      <script src="https://cdn.quilljs.com/1.3.6/quill.js"></script>
      <script src="script.js"></script>
 </body>
2. Create the ViewModel and the observable, Initialize the Quill Editor, and add the function that will be called when there is an action to the RTE, in this case, highlighted.
function ViewModel(){
    var self = this; 
    //Create an observable array to display the text that you highlight with your cursor.
    self.selectedText = ko.observableArray([]);
    
     //Initialize the Quill Editor
    var quill = new Quill('#editor', {
        theme: 'snow'
      });

    //This function will be called when you make a change in the text, in this case, highlighting a word. 
    quill.on('selection-change', function(range, oldRange, source){
        //This will get the text range that you highlighted.
        var text = quill.getText(range.index, range.length);
        //This changes the text to bold.
        quill.formatText(range.index, range.length, 'bold', true);
        self.selectedText.push(text);
    })
}
ko.applyBindings(new ViewModel());
3. Add a basic unordered list with a data-bind to display that data when it is highlighted, and that’s it!
<body>
    <ul data-bind="foreach: selectedText">
        <li data-bind="text: $data"></li>
    </ul>
    <div id="editor" style="max-height: 500px">
        <p>Hello World! My name is George, this is text that can be highlighted.</p>
        <p><br></p>
      </div>
</body>