Showing posts with label Eclipse. Show all posts
Showing posts with label Eclipse. Show all posts

Sunday, August 12, 2018

Angular 6 hello world example


In this post I'm going to setup angular 6 project and do small code demonstration for understanding the structure of the project.

Prerequisites 
  1. You should have install java 1.8 or above.
  2. You should have Eclipse installed in your PC.
  3. Your PC should setup Maven installed and configured.
 First of all lets install Angular and node in local machine. First we need to install node in local machine. You can download nodejs from following location. In my application I'm using node version node-v8.11.3-x64. Install it in your local machine. Then we can install Angular in local machine. Open command prompt and type following command and press enter it will install Angular in your local machine globally.

npm install -g @angular/cli

It will take around 1 minute to install Angular and now our machine ready to start the Angular developments.

Lets create Maven project to create our Hello World Angular application. Go to File -> New -> Maven Project and check on create simple project tick box as show below.



Then click next and provide Group ID ,Artifact ID and change the packaging to war as show in bellow screen shot. Then Click Finish.



Then go to main folder under src and right click on it then got to Show In -> Console then type following command to generate angular project under main folder.

ng new AngularHelloWorld

Then click enter and that command will create AngularHelloWorld project for you. Then you can refresh the project and see new folder has been created under main folder similar to bellow screen shoot.



Then go to AnngularHellowWorld - >src -> App and open the app.component.html folder and past following content inside it.

<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
  <h1>
    Welcome to Angular Hello World App!
  </h1>
</div>

Then again open Command Prompt and go to AngularHelloWorld folder and build the Angular Application by entering following commands.

ng build

Then you should able to see similar out put to bellow screen shot.


Then lets verify our application build successfully by refreshing project folder and you will be able to Angular generated output on dist folder under  AngularHelloWorld.


Then copy the content on AngularHelloWorld under dist folder and place it on the WebContent folder as show in bellow.


For this manual content copy we have to change one modification on index.html you have to change the following change.

<base href="/">
To
<base href="/AngularHelloWorld/">



Then lets and web.xml in to our project. Write click on webapp folder and new -> Folder and give folder name as WEB-INF. Then create web.xml file inside on it with following content.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Angular Web Application</display-name>
</web-app>

Final project structure would be similar to following figure.



Now write click on our AngularHelloWorld  project and go to run As -> Maven Install then you can see the generated war file on target folder. Now you can run your angular web application on tomcat server by deploying it on internal or external tomcat server.

Then you will be able to access your Angular web application on following URL http://localhost:8080/AngularHelloWorld/.

You will be able to see similar output as below figure.


You can access sample project in following GITHUB location.

Friday, December 9, 2016

Red Hat JBoss FUSE ActiveMQ simple example

In this post I'm going to show how to develop simple ActiveMQ queue producer and consumer. Then I will explain step by step guide to deploy it to the JBoss FUSE server. If you do not have JBoss Fuse setup in your local PC go through in my previous blog post related to setup the JBoss Fuse. While I'm learning the stuff it gave me little bit frustration due to the lack of resources to learn this technology as a beginner. Then I though to put it as blog post so in future any one can refer the guide line.

Prerequisites 
  1. You should have install java 1.8 to support latest jboss-fuse-6.3.0
  2. You should have install JBoss Developer studio
  3. Your PC should setup Maven installed and configured.

01) Setup the ActiveMQ queue in FUSE.

First we have to start the fuse server using fuse.bat in the "bin" folder inside the installation directory on Fuse. (In my previous post I have explain how to setup the Fuse server correctly. If you need to setup go through previous post before you continue)



Once it successfully started you can see similar interface to above figure. Then you have to type following command to configure the fabric.

fabric:create
Then you can see similar figure to below.



 Then go to yellow colored link "http://localhost:8181"(which showed in above figure) it will automatically redirect to "http://localhost:8181/hawtio/login". Then provide the user name and password which you should set when you setup the Fuse. 




Then Fuse will create queue for you and you can see generated queue under queue tag as show in below figure.



Now we have created the queue we need to demonstration. Then type following command on the Fuse command interface to get the ActiveMQ URI.

fabric:cluster-list
 Then you will see similar output to below.




Then you need to select the default URI related to root (Since our queue setup under root)

tcp://DESKTOP-3KNHDOV:61617

02) Develop Camel application to send data to ActiveMQ queue and read them in FUSE.

Then go to Eclipse with Fuse integration and go to File -> New -> Fuse Integration Project. Then provide name for the project. I'll provided it as "DemoActiveMq"  then click next and select the JBoss Fuse run-time environment then click next. Next Interface select Start with empty project and Spring DSL as show on below figure.


Then Go to the POM.xml file and update following two tags as show on below.

<groupId>com.amq.demo</groupId>
<artifactId>amq</artifactId>
These two will important when install the build jar in to Fuse if you use different names you may need to change the command according to your change names in installing the jar to Fuse server.

Remove the content inside the <beans> tag and add following content in to that.

<camelContext id="_camelContext1" xmlns="http://camel.apache.org/schema/spring">
        <route id="amq_test">
            <from id="_from1" uri="activemq:queue:DemoQueue"/>
            <log id="_log1" message="${body}"/>
            <log id="_log2" message="Finished"/>
        </route>
        <route id="_route1">
            <from id="_from2" uri="file:///c:/work/activemq"/>
            <to id="_to1" uri="activemq:queue:DemoQueue"/>
        </route>
    </camelContext>
    <bean class="org.apache.activemq.camel.component.ActiveMQComponent" id="activemq">
        <property name="brokerURL" value="tcp://DESKTOP-3KNHDOV:61617"/>
        <property name="userName" value="admin"/>
        <property name="password" value="admin"/>

    </bean>


Note that brokerURL is the URL which we retrieved previous stage and password is the password setup in Fuse server.  Final camel context will be similar to below.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
    <camelContext id="_camelContext1" xmlns="http://camel.apache.org/schema/spring">
        <route id="amq_test">
            <from id="_from1" uri="activemq:queue:DemoQueue"/>
            <log id="_log1" message="${body}"/>
            <log id="_log2" message="Finished"/>
        </route>
        <route id="_route1">
            <from id="_from2" uri="file:///c:/work/activemq"/>
            <to id="_to1" uri="activemq:queue:DemoQueue"/>
        </route>
    </camelContext>
    <bean class="org.apache.activemq.camel.component.ActiveMQComponent" id="activemq">
        <property name="brokerURL" value="tcp://DESKTOP-3KNHDOV:61617"/>
        <property name="userName" value="admin"/>
        <property name="password" value="admin"/>
    </bean>

</beans>

Then go to the project folder and give the following command to build and install the project as usual Fuse integration projects.


mvn clean install
Then you should receive similar output to below.



Then go to the Fuse command line and enter following command to install the build jar in to Fuse server. (Please note that this is based on the names I have updated in the pom.xml)


osgi:install -s mvn:com.amq.demo/amq/1.0.0-SNAPSHOT

After successful installation if you log in to the "http://localhost:8181/hawtio/" then you will be able to notice that new section added under our queue in UI. refer the below fegure.



Now we can check the flow by dropping txt file in to "C:\work\activemq" location with some sample content. I'll added below content in to the text file and drop the file in to folder.

<a>
<name>Nirmal</name>
<country>Sri Lanka</country>

</a>

Then you can see the following output as expected. 


We have completed our first ActiveMQ demo. You can access the project code on following github location.

Thursday, February 4, 2016

Java Persistance API (JPA) Hello world example with EclipseLink and Oracle

Java Persistance API (JPA) provides the interface for the java Object Relational Mapping (ORM) tools such as EclipseLink, OpenJPA, Hibernate etc. These ORM tools helps to change the application data bases without doing any code level changers on Aplication logics. In other words ORM tools reduce the coupling between application layer and database layer.

On this post I will describe step by step aproche to develop simple Java application using EclipseLink ORM tool. In another post I will explain how to do the same operation with Hibernate.

Prerequisites 
  1. You should have install java in your PC and set path variable correctly (JAVA_HOME, JRE_HOME)
  2. You should have install Eclipse Java EE LUNA 4.4 IDE or any IDE you preferred (In this post I'm Using Eclipse )
  3. You should have install Oracle on your PC. In my application I connect to the Oracle 11g database and based on that database vertion you may need to select proper databse connection driver.

Step 1 Lets create database table for our simple application
 First of all we have to have table with set of data. I will create student table using following sql.

create table student
(
studentid number,
firstName varchar2(20) not null,
lastName varchar2(20) not null,
age number(2),
primary key(studentid)
);


As you can see student will have studentid as primary key and firstName, lastName, age as other attributes.

Then I'll add some data in to this table using following SQL.

insert into student values(1,'Lakshan','Silva',21);
insert into student values(2,'Kuma','Sangakara',35);
insert into student values(3,'Nilakshi','Dissanayaka',29);


Then after retrive all the student details using "select * from student" SQL you may able to see similar result to below graph.


Our table is ready at the moment so lets move to next step.

Step 2 Lets Create Dynamic web project

First of all lets download the required jar files to this project. You should download the following jar files
1) javax.persistence-2.1.0-rc1.jar (http://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence/2.1.0-RC1)
2) ojdbc6.jar (Since I have using Oracle 11g I downloaded it from http://www.oracle.com/technetwork/apps-tech/jdbc-112010-090769.html)
 
 Go to File->New->Other select JPA project as show in below image.


Then click on Next and provide project name I use HelloworldJPA as my project name. Then select target runtime as installed JDK(In my case it's jdk1.7.0_17). Then select JPA version as 2.1 and click next.
At the end interface should be similar to below graph.


Then click next on interface ask you to select src folder( since we use default folder no need to do any modification). The you can see similar interface to below screen shot.



In this interface select platform as EclipseLink Then click on Manage User Libraries icon which show as No 1 red box in above image. Then click on new add user library name I use Javax.Persistence and then click Ok. The click on Add external Jars button and select the downloaded javax.persistence-2.1.0-rc1.jar then click OK.
Then lets add EclipseLink related jar files in to our project. To add that click on Download Library button(show on No 2 red box in above image). Then select the compatible EclipseLink library vertion and Click next. Then agree the agreement and download the relevant ExlipseLink jars in to project

Then lets add our database details for that click on Add connection then select connection profile type as Oracle and provide proper name for the connection I'm use "New Connection" 
Then provide details about database connection and click finish. (I have used oracle thin driver)
Then final image would be similar to below image.


 Then you may able to see project structure similar to below image.

Then double click on persistance.xml file and then go to connection tab. Select Transaction Type as Resource local. Then Click on Popular From Connection and select the database details which added on previous section. Then save the persistance.xml

Then we have to add databse drivers in to class path. Since in this example i'm using Oracle 11g I need to add ojdbc6.jar in to Build path. Right click on project and go to Build path -> Configure Build Path. Then click on Add External JARs and select the downloaded ojdbc6.jar. After adding database connecting driver in to project Java Build Path interface would be similar to below image. Then click Ok.



Step 3 Lets Create Entity class

In Object relational mapping process object table mapping done through the Entity class. On this mapping class we use different JPA anotations to spesify diferent kind of relationships on database tables.
Lets create new java class by right click on src folder and go to New-> class. then provide prefered class name with package name. I use class name as Student and package as "com.jpa.entity".

package com.jpa.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity(name="Student")
@Table(name="Student")
public class Student {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)

    @Column(name="studentid")
    int ID;
    @Column(name="firstName")
    String FirstName;
    @Column(name="lastName")
    String LastName;
    @Column(name="age")
    int Age;
   
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public String getFirstName() {
        return FirstName;
    }
    public void setFirstName(String firstName) {
        FirstName = firstName;
    }
    public String getLastName() {
        return LastName;
    }
    public void setLastName(String lastName) {
        LastName = lastName;
    }
    public int getAge() {
        return Age;
    }
    public void setAge(int age) {
        Age = age;
    }
   
    @Override
    public String toString() {
        return "Student [ID=" + ID + ", FirstName=" + FirstName + ", LastName="
                + LastName + ", Age=" + Age + "]";
    }
   
}

 

Then we have to spesify that we use EclipseLink as ORM tool and we use Student as entity class in persistance.xml. Final persistence.xml would similar to this.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="HellowWorldJPA" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.jpa.entity.Student</class>

        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
            <property name="javax.persistence.jdbc.user" value="DBUserName"/>
            <property name="javax.persistence.jdbc.password" value="DBPassword"/>
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
        </properties>
    </persistence-unit>
</persistence>
 


Step 4 Lets Create class to save details in to database using ORM tool
   
Right click on src folder and go to New-> class. Then provide prefered class name with package name. I use class name as MainApp and package as "com.jpa.mainApp". Then add following content in to that class.

package com.jpa.mainApp;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.jpa.entity.Student;

public class MainApp {
    public static void main(String[] args) {
       
        EntityManagerFactory emf=Persistence.createEntityManagerFactory("HellowWorldJPA");
        //"HellowWorldJPA" is the persistence unit name which we spesify in persistence.xml
        EntityManager em=emf.createEntityManager();
       
        em.getTransaction().begin();
       
        Student std=new Student();
        std.setID(4);
        std.setFirstName("Nilanka");
        std.setLastName("Subhash");
        std.setAge(35);
       
        em.persist(std);
        em.getTransaction().commit();
       
        em.close();
        emf.close();
    }
}



 Then lets run the class by right click on the class and go to Run As -> Java Application.
Then you should able to see similar log in colsole related to your project name and path details.

[EL Info]: 2016-02-04 17:56:10.874--ServerSession(1528705718)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[EL Info]: connection: 2016-02-04 17:56:12.674--ServerSession(1528705718)--file:/G:/Programming/LUNA_WK_SPACES/JPA_SAMPLE/HellowWorldJPA/build/classes/_HellowWorldJPA login successful
[EL Info]: connection: 2016-02-04 17:56:13.19--ServerSession(1528705718)--file:/G:/Programming/LUNA_WK_SPACES/JPA_SAMPLE/HellowWorldJPA/build/classes/_HellowWorldJPA logout successful


Then if you query student table you can see folowing output.


I will provide more advance features using Java Persistence API in my future posts. You can download the source code from following GIT location https://github.com/NirmalBalasooriya/HelloWorldJPA.