martes, 14 de diciembre de 2010

Java AMF Client: Using AMF with JSP

In this post i will be posting how to get the efficient AMF connection to work with a Java client specifically JSP, you can use any other java client but for this port i will use JSP.

There are times when you want an efficient protocol to transport data, ok you can use AMF but what about the client... most of people think that since AMF is a protocol created by Adobe you should use a Flash Player client to get this work, the true is that you could use another type of client like Java for example, so let see how can we use this.

BlazeDS has a class called AMFConnection this will help us to get the work done so let get stated:

 Requirements:

  • Eclipse IDE
  • BlazeDS: You must have some knowledge in setup BlazeDS, if not check my tutorial About Connect BlazeDS with Java (Spanish) or you can check this good tutorial of Technical Evangelist Sujit Reddy - Setting up BlazeDS.
  • MySQL and Mysql Driver.
So first create a database, call it test_db and a table called person, this table has the following columns:

Now populate the table with some data, if you work with little amount of data you can't see the real power of AMF, but for this example some records will be enough, here there is a small Sql script for doing that. 

In the Eclipse IDE, we need to import our BlazeDS war file and add the Mysql driver, i call the project AMF_Client, the project should look like this.


Now we create a package hierarchy for our project:
    

In the dto package create a Person class and add the following:

Person.java

package dto;

public class Person {
private int id_person;
private String first_name;
private String last_name;
private double salary;
public int getId_person() {
return id_person;
}
public void setId_person(int idPerson) {
id_person = idPerson;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String firstName) {
first_name = firstName;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String lastName) {
last_name = lastName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}


In our dao package create a DBHelper class for the connection, an interface PersonDAO and a class PersonDAOImp for the implementation.

DBHelper.java // There will be a lot of ways to do a database connection but i prefer this.

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Driver;

public class DBHelper {
private static Connection conexion;
private DBHelper(){
}
public static Connection getConnection() throws SQLException{
Class driver = Driver.class;
if(conexion == null){
try{
conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_db","","");
}catch(SQLException ex){
throw new SQLException(ex.getMessage());
}
}
return conexion;
}
}


PersonDAO.java

package dao;

import java.sql.SQLException;
import java.util.List;

import dto.Person;

public interface PersonDAO {
public List getPeople() throws SQLException;
}


PersonDAOImp.java


package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import dto.Person;

public class PersonDAOImp implements PersonDAO{
private Connection conexion;
@Override
public List getPeople() throws SQLException {
List people = new ArrayList();
try{
conexion = DBHelper.getConnection();
PreparedStatement pstmt = conexion.prepareStatement("SELECT * FROM person");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
Person person = new Person();
person.setId_person(rs.getInt(1));
person.setFirst_name(rs.getString(2));
person.setLast_name(rs.getString(3));
person.setSalary(rs.getDouble(4));
people.add(person);
}
}catch(SQLException ex){
throw new SQLException(ex.getMessage());
}
return people;
}
}    

Now create another class in the package services call this PersonService and put the following.

PersonService.java

package services;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.PersonDAO;
import dao.PersonDAOImp;
import dto.Person;

public class PersonService {
private PersonDAO personDAO = new PersonDAOImp();
public List getPeople() throws SQLException {
List people = new ArrayList();
try{
people = personDAO.getPeople();
}catch(SQLException ex){
throw new SQLException(ex.getMessage());
}
return people;
}
}

We need to configure BlazeDS so we can use the Java AMF Client in our JSP page, open the remoting-config.xml located in WebContent/WEB-INF/flex add the following:

remoting-config.xml
    

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service" 
    class="flex.messaging.services.RemotingService">
    <adapters>
        <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
    </adapters>
    <default-channels>
        <channel ref="my-amf"/>
    </default-channels>
<destination id="PersonService">
<properties>
<source>services.PersonService</source>
</properties>
</destination>
</service>



Now we are going to create a JSP page for handling the PersonService result.

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="flex.messaging.io.amf.client.AMFConnection"%>
<%@page import="flex.messaging.io.amf.client.exceptions.ClientStatusException"%>
<%@page import="java.util.List"%>
<%@page import="dto.Person"%>
<%@page import="java.util.ArrayList"%>
<%@page import="flex.messaging.io.amf.client.exceptions.ServerStatusException"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Java AMF Client</title>
</head>
<body>
<%
AMFConnection amfConexion = new AMFConnection();
String amfUrl = "http://localhost:8080/AMF_Client/messagebroker/amf";
try{
//Make the conexion
amfConexion.connect(amfUrl);
}catch(ClientStatusException ex){
out.write("An error has ocurred");
}
//Make the remote call to out service
List<Person> people = new ArrayList<Person>();
try{
people = ((ArrayList<Person>)amfConexion.call("PersonService.getPeople"));
}catch(ClientStatusException ex){
out.write("An error has ocurred");
}catch(ServerStatusException ex){
out.write("An error has ocurred");
}
%>
<table border="1">
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>Salary</td>
</tr>
<%
for(Person p : people){
%>
<tr>
<td><%out.print(p.getId_person());%></td>
<td><%out.print(p.getFirst_name());%></td>
<td><%out.print(p.getLast_name());%></td>
<td><%out.print(p.getSalary());%></td>
</tr>
<%
}
%>
</table>
</body>
</html>


Now your run the project and you should have something like this:


That's all you have running a project with Java AMF Client, remember the true power of AMF comes when you deal with large amounts of data, this tutorial only explains the basics with few rows. 

Here is the complete source code of the Project, any trouble or suggestion let me know. 




No hay comentarios:

Publicar un comentario