BlazeDS Simple Remoting (Part I – “Java”)

Requirements:

  • Flex Builder 2/3 (Since FlexBuilder is Eclipse Based we will use it for both flex and Java development)
    you can download trail version of FlexBuilder from following url : Flex Builder Trail Download
  • Java Web application Server (I use Tomcat in this example)
    you can download tomcat from following url: Tomcat Download

Server Side Tasks:

  • Create Simple Java web Application
  • Add BlazeDS Servlet definition…, Libraries, and Configuration files.
  • Create regular java Class, and add following two methods:
    • getAllEvents() returns List SportEvent Objects
    • getSportEvent(sportid) returns one SportEvent object (specific to sport id parameter)
  • Register Class with blaseDS so it can serve requests

Create java Web Application

Create java Web Project in Eclipse :
NOTE: I am using MyEclipse Plugin which is enhanced version of WTP, so screen shots should look similar :
Fine–> new Project –> dynamic web project (or just Web project)

Create Web ProjectCreate Web Project

Add Java beans

Event.java:(fragment)

JAVA:

  1. public class Event {
  2. private int id;
  3. private String name;
  4. private List<Team> teams;
  5. private Date eventDate;
  6. // default constructor
  7. //setters getters …
  8. }

Team.java(fragment)

JAVA:

  1. public class Team {
  2. private String name ;
  3. private String shortName;
  4. // default constructor
  5. //setters getters …
  6. }

Finally Lets Create Service class which will serve those beans to caller.
In this tutorial, we are not going to use DB so we need to hardcore some values to simulate random data:
EventsManager.java(fragment)

JAVA:

  1. public Event getSportEvent (int eid) throws Exception{
  2. // in reality :
  3. //you query db etc get correct event and return it.
  4. Event e = new Event();
  5. e.setEventDate(new Date());
  6. e.setId(eid);
  7. List<Team> l = new ArrayList<Team>();
  8. // Simulating the random Events
  9. switch (eid) {
  10. case 1:
  11. e.setName(event1);
  12. l.add(new Team(“Atlanta Hawks”,“ATL”) );
  13. l.add(new Team(“Los Angeles Lakers”,“LAL”) );
  14. break;
  15. case 2:
  16. case 3:
  17. default:
  18. break;
  19. }
  20. e.setTeams(l);
  21. return e;
  22. }
  23. public List<Event> getAllEvents() throws Exception{
  24. List<Event> ae = new ArrayList<Event>();
  25. ae.add(getSportEvent(1));
  26. ae.add(getSportEvent(2));
  27. return ae;
  28. }

Add BlazeDS to project :

  • Download latest Stable Build from following url: BlazeDS latest Build
    download Binary distribution version not turnkey version, for example this: http://flexorg.wip3.adobe.com/blazeds/3.0.x/milestone/3978/blazeds-bin-3.2.0.3978.zip
  • unzipp .war file with winrar or your proffered compression program .
  • copy jar files from blazeds/WEB-INF/lib directory to your projects web/WEB-INF/lib directory
  • create directory “flex” under your projects web/WEB-INF/ directory
  • Add two flex configuration files under flex directory :
    • services-config.xml
    • remoting-config.xml
WEB-INF directoryWEB-INF directory

Configure web.xml

Add following to your web.xml file:

XML:

  1. <!– MessageBroker Servlet –>
  2. <servlet>
  3. <servlet-name>MessageBrokerServlet</servlet-name>
  4. <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
  5. <init-param>
  6. <param-name>services.configuration.file</param-name>
  7. <param-value>/WEB-INF/flex/services-config.xml</param-value>
  8. </init-param>
  9. <load-on-startup>1</load-on-startup>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>MessageBrokerServlet</servlet-name>
  13. <url-pattern>/messagebroker/*</url-pattern>
  14. </servlet-mapping>

Configure services-config.xml add my-amf channel definition

XML:

  1. <services>
    <service-include file-path=”remoting-config.xml” />
    </services>
  2. <channel-definition id=“my-amf”
  3. class=“mx.messaging.channels.AMFChannel”>
  4. <endpoint url=http://{server.name}:{server.port}/{context.root}/messagebroker/amf”
  5. class=“flex.messaging.endpoints.AMFEndpoint”/>
  6. </channel-definition>

Configure remoting-config.xml add destination for our SportEventsManager class

XML:

  1. <destination id=“sportEventsManager”>
  2. <properties>
  3. <source>com.example.EventsManager</source>
  4. </properties>
  5. </destination>

lets test the settings, build and deploy java project to tomcat and access following url :

http://localhost:8080/your_app_context_name/messagebroker/amf

you should get empty page which means blazeDS is running and waiting for request.
that’s all for java part.

(Source: http://ledtechdesign.com/2009/02/blazeds-tutorial-part-i-simple-remoting/)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: