Friday, March 16, 2018

Install Nodejs and NPM Ubuntu 17

Step 1: Run the command to install LTS Release version.

 sudo apt-get install python-software-properties  
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -  
sudo apt-get install -y nodejs

Then verify your npm and nodejs installation.
 node -v   
 npm -v  
Useful link
  Nodejs

Thursday, March 15, 2018

Share your docker image

Before Moving forward you have to define your container with Dockerfile. If you haven't done that follow the link Define your container with Dockerfile

Now it time to share your image across multiple platform and team using a docker registries. A registry is a collection of repositories, and a repository is a collection of images—sort of like a GitHub repository, except the code is already built.

Step 1: Make account on cloud.docker.com
Docker’s public registry is free and pre-configured, but there are many public ones to choose from.
Note that the docker CLI uses Docker’s public registry by default.

Login using docker CLI

 docker login  

After your are logged in successfully

Step 2: Tag the Image
Tagging image is good way of associating a local image with a repository on a registry.
For e.g. my-repo:my-first-image this puts the image in the my-repo repository and tag it as my-first-image.

 docker tag image username/repository:tag  

 docker tag my-app lamadipen/my-repo:first  
View list of images

 docker image ls  

Step 3: Publish your  image

  sudo docker push lamadipen/my-repo:first  

Now you can find your image on Docker Hub
Docker hub screen shot

Step 4: Pull the image and run image from the remote repository any where you want.
 docker run -p 4000:80 username/repository:tag  
For example
 docker run -p 4000:80 username/repository:tag  

This command pull your image from the repository if image isn't available on local machine and run it on your .

Step 5: Check your container serving you application
You will get response from your application on the browser if you check the link in your browser.
 http://localhost:8000/  

Tuesday, March 13, 2018

Define your container with Dockerfile

If you haven't installed Docker, then install it on your machine using the link (Installing Docker on Ubuntu).

Define a container with Dockerfile

Now its time to create your own custom environment using DockerFile. DockerFile behaves exactly the same wherever it runs enabling you to establish same standard configuration on dev, test and production environment.

Step 1: Create a folder (in my case i have created my-docker-img folder)
Step 2: Create a Dockerfile inside the folder (I have created Dockerfile.txt)
Dockerfile.txt
 # Use an official Python runtime as a parent image  
 FROM python:2.7-slim  
 # Set the working directory to /app  
 WORKDIR /app  
 # Copy the current directory contents into the container at /app  
 ADD . /app  
 # Set proxy server, replace host:port with values for your servers (If your are behind proxy server)  
 #ENV http_proxy host:port  
 #ENV https_proxy host:port  
 # Install any needed packages specified in requirements.txt  
 RUN pip install --trusted-host pypi.python.org -r requirements.txt  
 # Make port 80 available to the world outside this container  
 EXPOSE 80  
 # Define environment variable  
 ENV NAME World  
 # Run app.py when the container launches  
 CMD ["python", "app.py"]  

Step 3: Create app.py and requirement.txt specified in Docker file in the same folder.
app.py file is your python application that you want to deploy

app.py

 from flask import Flask  
 from redis import Redis, RedisError  
 import os  
 import socket  
 # Connect to Redis  
 redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)  
 app = Flask(__name__)  
 @app.route("/")  
 def hello():  
   try:  
     visits = redis.incr("counter")  
   except RedisError:  
     visits = "<i>cannot connect to Redis, counter disabled</i>"  
   html = "<h3>Hello {name}!</h3>" \  
       "<b>Hostname:</b> {hostname}<br/>" \  
       "<b>Visits:</b> {visits}"  
   return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)  
 if __name__ == "__main__":  
   app.run(host='0.0.0.0', port=80)  

requirements.txt
 Flask  
 Redis  

Note:
When the Dockerfile is built into an image, Dockerfile’s ADD command will add app.py into the image, and the app is served over HTTP with help the EXPOSE command which expose port 80. PIP command install python dependency libraries specified on requirements.txt file

Build the app

Now execute the build command from top of the directory. This creates a Docker image with tag using -t so it has a friendly name.
In my case directory is /Documents/my-docker-img and image name is my-app
 docker build -t my-app .  
To view the list images created on your machine
 docker image ls  

Run the app

Run the app, by mapping your machine’s port 8000 to the container’s published port 80 using -p
 docker run -p 4000:80 friendlyhello  
Then you can access your app deployed within the container using following URL
 http://localhost:8000/  
If you want to run the container in the background in detach mode
 docker run -d -p 4000:80 my-app  
To view all the running containers on your machine
 docker container ls  
If you want to stop the running container using CONTAINER ID
  sudo docker container stop c1081125c6e9   

Next Share your docker image

Useful links
https://docs.docker.com/get-started/part2/#log-in-with-your-docker-id

Installing Docker in Ubuntu

Set up repository to install dependent package for Docker installation
 sudo apt-get update  
Install packages to allow apt to use a repository over HTTPS:
  sudo apt-get install \  
   apt-transport-https \  
   ca-certificates \  
   curl \  
   software-properties-common  
Add Docker’s official GPG key:
 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -  
To Verify the key with the fingerprint by matching the last 8 characters of the fingerprint.
 sudo apt-key fingerprint 0EBFCD88  
Set up the stable repository I have chosen stable repository and x86_64/amd64. For different version please follow the link
 sudo add-apt-repository \  
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \  
   $(lsb_release -cs) \  
   stable"  
Now to install Docker
Update the apt package index
 sudo apt-get update  
Install the latest version of Docker CE
 sudo apt-get install docker-ce  
To verify that Docker CE is installed correctly by running the hello-world image
 sudo docker run hello-world  
It will download a test image and run it in a container. When the container runs, it prints an informational message and exits. Now Docker CE is installed and running on your machine.

If you need to uninstall Docker installed
 sudo apt-get purge docker-ce  
To delete all images, containers, and volumes which is not removed automatically
 sudo rm -rf /var/lib/docker  
If you want to configure Linux as post installation Click the Link. Some Useful Commands: To see list of images downloaded image on your machine.
 docker image ls  
To get help
 docker container --help  
Next Define your own container with Docker file

Helpful links
https://docs.docker.com/get-started/#conclusion-of-part-one
https://github.com/docker/labs/tree/master/beginner/

Friday, May 5, 2017

JMS implementation

In this example I am creating an JMS implementation of  Point to Point Messaging Domain.

Step 1: Create a simple maven project using you favorite IDE. I am using IntelliJ




Below I have mentioned dependencies required for the project. Add the dependency in your pom.xml

pom.xml
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.dipenjms</groupId>  
  <artifactId>JMS</artifactId>  
  <version>1.0-SNAPSHOT</version>  
   <build>  
     <plugins>  
       <plugin>  
         <groupId>org.apache.maven.plugins</groupId>  
         <artifactId>maven-compiler-plugin</artifactId>  
         <configuration>  
           <source>1.7</source>  
           <target>1.7</target>  
         </configuration>  
       </plugin>  
     </plugins>  
   </build>  
   <packaging>jar</packaging>  
  <name>JMS</name>  
  <url>http://maven.apache.org</url>  
  <properties>  
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  </properties>  
  <dependencies>  
   <!-- https://mvnrepository.com/artifact/javax.jms/javax.jms-api -->  
   <dependency>  
    <groupId>javax.jms</groupId>  
    <artifactId>javax.jms-api</artifactId>  
    <version>2.0</version>  
   </dependency>  
   <dependency>  
    <groupId>junit</groupId>  
    <artifactId>junit</artifactId>  
    <version>3.8.1</version>  
    <scope>test</scope>  
   </dependency>  
  </dependencies>  
 </project>  


Step 2:
Create a MyReceiver class which is a message consumer.
Note: java:comp/DefaultJMSConnectionFactory in initialContext.lookup() must match name Logical JNDI Name in server and also queue name.

MyReceiver.java
 package javatpoint.dipen.jms;  
 import javax.jms.*;  
 import javax.naming.InitialContext;  
 import javax.naming.NamingException;  
 /**  
  * Created by dipen on 5/5/2017.  
  */  
 public class MyReceiver {  
   public static void main(String[] args) {  
     try {  
       InitialContext initialContext = new InitialContext();  
       QueueConnectionFactory queueConnectionFactory= (QueueConnectionFactory) initialContext.lookup("java:comp/DefaultJMSConnectionFactory");  
       QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();  
       queueConnection.start();  
       QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);  
       Queue queue = (Queue) initialContext.lookup("myQueue");  
       QueueReceiver queueReceiver = queueSession.createReceiver(queue);  
       final TextMessage[] textMessage = new TextMessage[1];  
       queueReceiver.setMessageListener(   new MessageListener(){  
         @Override  
         public void onMessage(Message message) {  
           System.out.print("Inside Message Receiver" + message);  
           textMessage[0] = (TextMessage) message;  
         }  
       });  
       System.out.print(textMessage[0]);  
       queueConnection.close();  
     } catch (NamingException | JMSException e) {  
       e.printStackTrace();  
     }  
   }  
 }  

Step 3:
Create a MySender class which is a message producer.

MySender.java
 package javatpoint.dipen.jms;  
 import javax.jms.*;  
 import javax.naming.Context;  
 import javax.naming.InitialContext;  
 import javax.naming.NamingException;  
 import javax.xml.soap.Text;  
 import java.util.Properties;  
 /**  
  * Created by dipen on 5/5/2017.  
  */  
 public class MySender {  
   public static void main(String[] args) {  
     Properties env = new Properties( );  
     env.put(Context.INITIAL_CONTEXT_FACTORY,  
         "com.sun.jndi.fscontext.RefFSContextFactory");  
     /* env.put(Context.PROVIDER_URL,  
         "ts://localhost:4848");*/  
     try {  
       InitialContext initialContext = new InitialContext();  
       QueueConnectionFactory queueConnectionFactory= (QueueConnectionFactory) initialContext.lookup("java:comp/DefaultJMSConnectionFactory");  
       QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();  
       queueConnection.start();  
       QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);  
       Queue queue = (Queue) initialContext.lookup("myQueue");  
       QueueSender queueSender = queueSession.createSender(queue);  
       TextMessage textMessage = queueSession.createTextMessage();  
       textMessage.setText("Hello Dipen! This is JMS Testing");  
       queueSender.send(textMessage);  
       queueConnection.close();  
     } catch (NamingException | JMSException e) {  
       e.printStackTrace();  
     }  
   }  
 }  

Step 4:
Install  Glassfish4 Server from official site or Click Here and unzip in your desired directory.
Note: Download Java EE 7 Full Platform version.

Open bin directory from recently extracted Server directory and run asadmin.bat
For me it is: F:\IDE\Servers\glassfish4\bin

Then start the server using the command: start-domain



Now on your browser load the server admin page :http://localhost:4848/common/index.jsf


Step 5:
Click on the JMS Resource -> Connection Factories -> New, now write the pool name and select the Resource Type as QueueConnectionFactory then click on ok button.


Click on the JMS Resource -> Destination Resources -> New, now write the JNDI name and physical destination name then click on ok button.


Step 6: Run both MySender and MyReceiver.

Note : If there is error while running the project, add javaee.jar and appserv-rt.jar into your library from <yourpath>\glassfish\lib
For me it's: F:\IDE\Servers\glassfish4\glassfish\lib

Sender output

Receiver output

Download Source for GitHub : Click Here

Sunday, April 23, 2017

Installing and setting Java path


Download Java
Download lastest release of java form the official site
http://www.oracle.com/technetwork/java/javase/downloads/index.html
 Install the downloaded file
The file is installed into follwing location C:\Program Files\Java
Then, right click on mycomputer




 -> click properties -> the follwing screen will be displayed


 Now, click on Advance System Setting, screen below appears



 Click on Envirnment Variables, another window is displayed



 Click New button below the upper box and secreen below will be displayed

Insert following into corrosponding textbox
Variable name: JAVA_HOME
Variable Value: C:\Program Files\Java\jdk1.7.0_07





In lower box select Path and click Edit button, at the end after “;” paste full path to bin folder (Note :- keep other content in box as it is and end the line with “;”)
Variable Value: C:\Program Files\Java\jdk1.7.0_07\bin;



Now, after all this process check that Java installation and setting is successfully completed
Open command  and run follwing syntax
C:\Java -version and you will see screen below if all the setting are correct


Check code quality with PMD

Integrating PMD plugin on STS (subclipse) /Eclipse

1. Open STS  -> click on Help


2. Click ->Install New software



3. Click Add Button and screen blow is dispalyed
Name : PMDplugin
Location: http://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site/

4. Check on PMD for Eclipse 3.

5. Click -> Next

6. Agree the agreement and click -> Finish


7. Wait for Eclipse to download the required jar files, then click Install
8. Restart the workbench. This will load the PMD plugin.
9. Navigate to Window | Show View | Other...

10. Select PMD |select -> Violations overview


Opearating  PMD in STS  (How to usePMD)
1. In the Package Explorer right-click on Project and select PMD | Check Code With PMD

2. PMD Violations view notice a list of violations



SQL INTERVIEW QUESTIONS AND ANSWERS