Java Tutorial : Hands on project for library management application

I am coding Library Management Project in Java and teaching it in simple steps.
If you are interested in doing or learning Java with hands on project then check out this play list from my youtube video 🙂

Git Repo :
https://github.com/ProjectsbyPrem/LibraryManagement

Project requirements:
https://docs.google.com/document/d/1ZzAuMQ5jqCwh1qvF5DIvuVP6J1QwUugTuQ1uQjIXRkg/edit
For details visit : http://www.premaseem.com

MongoDB : Java program to do basic crud operation

Sample Java program to connect MongoDb and perform Crud Operations.

Copy paste, run and learn 😉

note :- Please ensure you have installed mongo dependency using jar, github or mvn

 

package com.premaseem.core;

import com.mongodb.*;

import java.util.Date;

/**
 * Java + MongoDB Crud operation example
 *
 */
public class App {
    public static void main(String[] args) {

        try {

            /**** Connect to MongoDB ****/
            // Since 2.10.0, uses MongoClient
            MongoClient mongo = new MongoClient("localhost", 27017);

            // or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members
//            MongoClient mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),
//                    new ServerAddress("localhost", 27018),
//                    new ServerAddress("localhost", 27019)));

            /**** Get database ****/
            // if database doesn't exists, MongoDB will create it for you
            DB db = mongo.getDB("testdb");

            /**** Get collection / table from 'testdb' ****/
            // if collection doesn't exists, MongoDB will create it for you
            DBCollection table = db.getCollection("user");

            /**** Insert ****/
            // create a document to store key and value
            BasicDBObject document = new BasicDBObject();
            document.put("name", "premaseem");
            document.put("age", 30);
            document.put("createdDate", new Date());
            table.insert(document);

            /**** Find and display ****/
            BasicDBObject searchQuery = new BasicDBObject();
            searchQuery.put("name", "premaseem");

            DBCursor cursor = table.find(searchQuery);

            while (cursor.hasNext()) {
                System.out.println(cursor.next());
            }

            /**** Update ****/
            // search document where name="premaseem" and update it with new values
            BasicDBObject query = new BasicDBObject();
            query.put("name", "premaseem");

            BasicDBObject newDocument = new BasicDBObject();
            newDocument.put("name", "premaseem-updated");

            BasicDBObject updateObj = new BasicDBObject();
            updateObj.put("$set", newDocument);

            table.update(query, updateObj);

            /**** Find and display ****/
            BasicDBObject searchQuery2
                    = new BasicDBObject().append("name", "premaseem-updated");

            DBCursor cursor2 = table.find(searchQuery2);

            while (cursor2.hasNext()) {
                System.out.println(cursor2.next());
            }

            /**** Done ****/
            System.out.println("Done");

        }  catch (MongoException e) {
            e.printStackTrace();
        }

    }
}

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.