Database Tutorial DB Application

From RifidiWiki

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

Now that we have built a query, we need to write a listener to the query which will execute whenever a query returns a result.

queryAllTags.addListener(new StatementAwareUpdateListener() {
	@Override
	public void update(EventBean[] arg0, EventBean[] arg1,EPStatement arg2, EPServiceProvider arg3) {
		if (arg0 != null) {
			for (EventBean b : arg0) {
				Object o = b.getUnderlying();

				// if we have seen a tag, create a new RFIDEvent, and
				// add it to the DAO
				if (o instanceof TagReadEvent) {
					TagReadEvent tre = (TagReadEvent) o;
					RFIDEvent e = new RFIDEvent();
					e.setAntenna(tre.getAntennaID());
					e.setId(((EPCGeneration1Event) tre.getTag()).getEpc());
					e.setTimestamp(tre.getTimestamp());
					e.setReader(tre.getReaderID());
					rfidDAO.addRow(e);
				}
			}
		}
	}
});

This listener simply extracts the information out of the TagReadEvent object that esper gives us in the EventBean and puts it into an RFIDEvent object (which is a data container object which holds the data we want to put into the database). We then give the newly created RFIDEvent object to the DAO (the object that interfaces with the database).

Extra Credit

If you are looking to extend the DB Application in some way, here are a few ideas:

More Complicated Esper Query

The esper query is about as simple as it gets for this particular application. You could, however, take advantage of esper to filter out unique tag events, or only log events from certain readers for example. For more examples of how to use esper, see the Northwind Tutorial.

Personal tools