Java : Running Multiple Tomcat Instances on One Machine

Download multiple version of apache tomcat and unzip them (prefer to download different version)

Key is to do proper setting in server.xml and run startup.sh in all tomcat server. We need to ensure below 3 ports are configured different in all multiple servers

  • Connector Port : This is the port where Apache Tomcat listen for the HTTP requests.
  • Shutdown Port : This port is used when we try to shutdown the Apache Tomcat Server.
  • AJP (Apache JServ Protocol) Connector Port : The Apache JServ Protocol (AJP) is a binary protocol that can conduct inbound requests from a web server through to an application server that sits behind the web server.

 

Locate server.xml in {Tomcat installation folder}\ conf \server.xml
Below port needs to be different for your servers

<Server port=”8015″ shutdown=”SHUTDOWN”>

<Connector port=”8181″ protocol=”HTTP/1.1″
connectionTimeout=”20000″
redirectPort=”8443″ />

<Connector port=”8019″ protocol=”AJP/1.3″ redirectPort=”8443″ />

Java : How to setup and run activeMQ for JMS on macos

Setup using command of home brew : 

➜ software brew install apache-activemq
==> Downloading https://www.apache.org/dyn/closer.cgi?path=/activemq/5.13.0/apache-activemq-5.13.0-bin.tar.gz
==> Best Mirror http://apache.spinellicreations.com/activemq/5.13.0/apache-activemq-5.13.0-bin.tar.gz

curl: (28) Resolving timed out after 5539 milliseconds
Trying a mirror…
==> Downloading https://archive.apache.org/dist/activemq/5.13.0/apache-activemq-5.13.0-bin.tar.gz
######################################################################## 100.0%
🍺 /usr/local/Cellar/activemq/5.13.0: 559 files, 58.9M, built in 2 minutes 41 seconds

 

Go to default directory of active mq 

When you use brew the default installation directory on mac os is below
➜  software cd /usr/local/Cellar/activemq/

Starting the activemq service 

➜ bin git:(master) activemq start
INFO: Loading ‘/usr/local/Cellar/activemq/5.13.0/libexec//bin/env’
INFO: Using java ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java’
INFO: Starting – inspect logfiles specified in logging.properties and log4j.properties to get details
INFO: pidfile created : ‘/usr/local/Cellar/activemq/5.13.0/libexec//data/activemq.pid’ (pid ‘58039’)

Please verifiy the service is running using below command
➜  bin git:(master) ps -eaf | grep activemq

Testing the activemq service on bowser

Open below url and use id as “admin” and password as “admin” to login
http://127.0.0.1:8161/admin/

Stopping the serving 

➜ bin git:(master) activemq stop
INFO: Loading ‘/usr/local/Cellar/activemq/5.13.0/libexec//bin/env’
INFO: Using java ‘/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java’
INFO: Waiting at least 30 seconds for regular process termination of pid ‘58039’ :
Java Runtime: Oracle Corporation 1.8.0_60 /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre
Heap sizes: current=62976k free=61664k max=932352k
JVM args: -Xms64M -Xmx1G -Djava.util.logging.config.file=logging.properties -Djava.security.auth.login.config=/usr/local/Cellar/activemq/5.13.0/libexec//conf/login.config -Dactivemq.classpath=/usr/local/Cellar/activemq/5.13.0/libexec//conf:/usr/local/Cellar/activemq/5.13.0/libexec//../lib/ -Dactivemq.home=/usr/local/Cellar/activemq/5.13.0/libexec/ -Dactivemq.base=/usr/local/Cellar/activemq/5.13.0/libexec/ -Dactivemq.conf=/usr/local/Cellar/activemq/5.13.0/libexec//conf -Dactivemq.data=/usr/local/Cellar/activemq/5.13.0/libexec//data
Extensions classpath:
[/usr/local/Cellar/activemq/5.13.0/libexec/lib,/usr/local/Cellar/activemq/5.13.0/libexec/lib/camel,/usr/local/Cellar/activemq/5.13.0/libexec/lib/optional,/usr/local/Cellar/activemq/5.13.0/libexec/lib/web,/usr/local/Cellar/activemq/5.13.0/libexec/lib/extra]
ACTIVEMQ_HOME: /usr/local/Cellar/activemq/5.13.0/libexec
ACTIVEMQ_BASE: /usr/local/Cellar/activemq/5.13.0/libexec
ACTIVEMQ_CONF: /usr/local/Cellar/activemq/5.13.0/libexec/conf
ACTIVEMQ_DATA: /usr/local/Cellar/activemq/5.13.0/libexec/data
Connecting to pid: 58039
………………………….
INFO: Regular shutdown not successful, sending SIGKILL to process
INFO: sending SIGKILL to pid ‘58039’

Other tasks which can run as
$ activemq <taskname>

Tasks:
browse – Display selected messages in a specified destination.
bstat – Performs a predefined query that displays useful statistics regarding the specified broker
consumer – Receives messages from the broker
create – Creates a runnable broker instance in the specified path.
decrypt – Decrypts given text
dstat – Performs a predefined query that displays useful tabular statistics regarding the specified destination type
encrypt – Encrypts given text
export – Exports a stopped brokers data files to an archive file
list – Lists all available brokers in the specified JMX context
producer – Sends messages to the broker
purge – Delete selected destination’s messages that matches the message selector
query – Display selected broker component’s attributes and statistics.
start – Creates and starts a broker using a configuration file, or a broker URI.
stop – Stops a running broker specified by the broker name.

How to install maven on Mac ?

Use this simple command : $ brew install maven

Below is sample command output

➜ aws-java-sample git:(master) brew install maven
==> Downloading https://www.apache.org/dyn/closer.cgi?path=maven/maven-3/3.3.9/b
==> Best Mirror http://download.nextag.com/apache/maven/maven-3/3.3.9/binaries/a
######################################################################## 100.0%
🍺 /usr/local/Cellar/maven/3.3.9: 94 files, 9.6M, built in 51 seconds
➜ aws-java-sample git:(master)

To validate if it is installed successfully : $  mvn -version 

Below is sample command output

➜ aws-java-sample git:(master) mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T10:41:47-06:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_60, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: “mac os x”, version: “10.10.5”, arch: “x86_64”, family: “mac”
➜ aws-java-sample git:(master)

 

What is ISO date format

As world is becoming a big family with globalization, we need the standard to followed to avoid confusion especially related to date and time. If every country or person would write dates in different format, it would be confusing to figure out exact date while doing communication across countries, hence ISO has come up with ISO date format to be followed by every one. 

International Standard ISO 8601 specifies numeric representations of date and time. This standard notation helps to avoid confusion in international communication caused by the many different national notations and increases the portability of computer user interfaces.

YYYY-MM-DDThh:mm:ss.sTZD

where:

     YYYY = four-digit year

     MM   = two-digit month (01=January, etc.)

     DD   = two-digit day of month (01 through 31)

     hh   = two digits of hour (00 through 23) (am/pm NOT allowed)

     mm   = two digits of minute (00 through 59)

     ss   = two digits of second (00 through 59)

     s    = one or more digits representing a decimal fraction of a second

     TZD  = time zone designator (Z or +hh:mm or -hh:mm)

example: 

 1997-07-16T19:20:30.45Z (Z at end indicates time is taken with Zero correction i.e. UTC time)

 1997-07-16T19:20:30.45+01:00 (+1:00 at end indicates time is taken at time zone which is 1:00 ahead of UTC time)

Advantages of the ISO 8601 standard date notation compared to other commonly used variants:

  • easily readable and writeable by software (no ‘JAN’, ‘FEB’, … table necessary)
  • easily comparable and sortable with a trivial string comparison
  • language independent
  • can not be confused with other popular date notations
  • consistency with the common 24h time notation system, where the larger units (hours) are also written in front of the smaller ones (minutes and seconds)
  • strings containing a date followed by a time are also easily comparable and sortable (e.g. write “1995-02-04 22:45:00”)
  • the notation is short and has constant length, which makes both keyboard data entry and table layout easier
  • identical to the Chinese date notation, so the largest cultural group (>25%) on this planet is already familiar with it 🙂
  • date notations with the order “year, month, day” are in addition already widely used e.g. in Japan, Korea, Hungary, Sweden, Finland, Denmark, and a few other countries and people in the U.S. are already used to at least the “month, day” order
  • a 4-digit year representation avoids overflow problems after 2099-12-31

API : Text messages from https://www.twilio.com/

If you need text / sms notification or alerts to be triggered by your program when https://www.twilio.com/ is the best option. You can consume it in any language like java, python, node etc. Simple to use and very effective.

Here is its code base in python : https://github.com/twilio/twilio-python

Use case :

  1. Bank account it going through some suspicious activities and need text alert.
  2. Text appointment reminder to customers.
  3. Text status of services etc.

Sample code of usage.

__author__ = ‘asee2278’
import twilio

from twilio.rest import TwilioRestClient

# put your own credentials here
ACCOUNT_SID = “AC72b94808df4448e5923b27258493df48”
AUTH_TOKEN = “XXXXXXXXXXXX”

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)

message = client.messages.create(
to=”+13045047736″,
from_=”+16814044918″,
body=”Test message “,
)

print message.sid

How to write and execute mongoDB scripts

There are times when we need to save the steps or commands of mongo shell and need to get it executed in the same order (for reusability and automation and avoid errors). The best solution is to save it in a file with any extension preferred is js, however I call it as mjs which means mongo javascript. With below command you can run it in bash

aseem278$ mongo < /Users/asee2278scripts/mongoScript.mjs

TIP : To be effective while writing query, you write commands using IDE to use auto complete, formatting, Json format verification and then redirect input to mongo 

Sample mongoScript.mjs

use school

db.scores.drop()

var types = [‘exam’, ‘homework’, ‘quiz’]

for (student_id = 0; student_id < 100; student_id++) {

    for (type=0; type < 3; type++) {

   var r = {‘student_id’:student_id, ‘type’:types[type], ‘score’:Math.random() * 100};

   db.scores.insert(r);

    }

}

Java Concept : No need to initialize boolean at class level

This concept says, boolean take the default value and does not required any pre-assignment if at class level
How ever if it is a local variable ie defined inside the method then compilation error for uninitialized variable might occur.

boolean

If we do like :

boolean var = false;

Then it reassign the same value in 2 steps and make is much more slower.

1. Assign default value (false) to variable. 

2. Assign user defined value to variable.

For better performance and clean code leave assignment to JVM Smile