RPC

BlazeDS features Remote Procedure Call services to support a call and response model.

A client-side RPC component calls a remote service. The component then stores the response data from the service in an ActionScript object from which you can easily obtain the data. The client-side RPC components used here are RemoteObjects.

RTM

BlazeDS messaging service uses the simple publish and subscribe method. Client applications communicate asynchronously by passing messages back and forth through the server.

Client applications that send messages are called producers. Client applications that receive messages are called consumers.

In Flex a consumer component is defined by using the Consumer component.

There are non-polling and polling patterns to simulate real-time messaging. The personFeed service uses real time messaging by using a streaming channel which provides true data streaming.

Person case - Person back-end feed service

The personFeedService back-end is implemented in a java class which spawns a messaging thread.

For communicating the java object uses the defaultMessageTemplate defined in the Spring flex context.


/*
 *
 * The person push service builds up a random list of persons which send to any consumers.
 * Before any sending the list is regenerated randomly.
 *
 */
public class PersonPushService {
    private MessageTemplate ioTemplate;
	private Random ioRandom;
	private PersonGenerator ioThread;
    private Logger ioLogger = Logger.getLogger(PersonPushService.class);

    /*
     * Class constructor with message template intitialization
     *
     * @param poMessageTemplate the message where the push data is fitted in
     *
     */
	public PersonPushService(MessageTemplate poMessageTemplate){
		ioRandom = new Random();
        ioTemplate = poMessageTemplate;
		ioLogger.debug("PersonPush service initilized");
	}

    /*
     * Method to start push service
     */
    public void start(){
		if(ioThread == null){
			ioLogger.debug("PersonPush service started");
			ioThread = new PersonGenerator(ioTemplate);
			ioThread.start();
		}
	}

    /*
     * Method which stops push service
     */
	public void stop(){
		ioLogger.debug("PersonPush service stopped");
		ioThread.running = false;
		ioThread=null;
	}
		

The thread class generates every 5 seconds a new message and sends it to the correct message destination personFeed.


    /*
     * Thread class for pushing the persons
     */
	public class PersonGenerator extends Thread {
        private MessageTemplate ioPGMTemplate;
		public boolean running = false;

        /*
         * Class contructor with message template
         *
         * @param poMessageTemplate the message layout template
         */
        public PersonGenerator(MessageTemplate poMessageTemplate)
        {
            ioPGMTemplate = poMessageTemplate;
        }

        /*
         * Method run
         */
		public void run(){
            running = true;
            DateFormat loDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

			while (running){
				List <PersonType>a= generatePersons();
                Date date = new Date();
                ioLogger.debug("PersonPush service sending message on " + loDateFormat.format(date));
                ioPGMTemplate.send("personFeed", a);

				try{
					Thread.sleep(5000);
				}catch(InterruptedException e){
					ioLogger.error("Exception: " + e.getMessage());
					e.printStackTrace();
				}
			}

		}
	}