Database Tutorial DB Application

From RifidiWiki

Revision as of 19:59, 3 December 2009 by Kyle (Talk | contribs)

Jump to: navigation, search

This page explains a few aspects of the DBApp class and it could be extended. The DBApp class has two main pieces of functionality:

  1. Control the lifecycle of the application. It has a start and stop method to do this.
  2. Submit esper statements to the esper runtime and handle events.

Activator

The Activator is a class that implements the BundleActivator interface. It has two methods: start and stop. These methods are called by the OSGi runtime when the bundle is started and stopped. One thing to keep in mind about the DBApp is that we need to some clean up when the DBApp bundle is stopped. Therefore in the constructor of the the DBApp class, we give a reference to the Activator:

	/**
	 * The constructor
	 */
	public DBApp() {
		Activator.myApp = this;
	}

Then in the Activator, we can call the stop method of the DBApp when the bundle's stop method is called:

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		myApp.stop();
	}

Dependency Injection

The DBApp class needs two important objects to work

EsperManagementService
This is the object that allows us to interact with the esper runtime.
RFID_DAO
This is a Data Access Object (DAO) that allows us to interact with the database.

The Rifidi Edge Server encourages the use of dependency injection using spring to get a hold of objects. To use dependency injection, just create public setter methods that take in the object to be injected. For example:

	/**
	 * Called by spring
	 * 
	 * @param esperService
	 *            the esperService to set
	 */
	public void setEsperService(EsperManagementService esperService) {
		this.esperService = esperService;

	}

	/**
	 * Called by spring
	 * 
	 * @param rfidDAO
	 *            the rfidDAO to set
	 */
	public void setRfidDAO(RFID_DAO rfidDAO) {
		this.rfidDAO = rfidDAO;
	}

We will configure which objects get injected in the spring.xml file.

Esper Query

Querying Event Objects
The Esper Query Language is similar to SQL in syntax. However, with Esper, you query streams of objects rather than tables in a database. This means that to develop Esper queries, you need to know the underlying structure of the objects (just like you need to know table and column names to form SQL queries). Notice in this example query, we are querying ReadCycle objects. To know more about the structure of the ReadCycle object see ReadCycle Class Hierarchy

Esper is an event stream processing engine. It allows us to query RFID events as they happen in real time. All RFID events produced by the Rifidi Edge Server Sensor Abstraction Layer are put into esper. We can then create applications (such as this one) that query the events and take some action when one that we are interested in happens.

For this application, we are just logging to a database every tag that is seen by a reader. Our esper statement in this case is simple:

EPStatement queryAllTags = esperService.getProvider().getEPAdministrator().createEPL(
	"select * from ReadCycle[select * from tags]");

Esper Listener

Personal tools