Jobs And Services

Life is, and always has been, about executing a sequence of jobs – hunt mammoth, skin mammoth, cook mammoth, make clothes from mammoth. In the beginning an individual would have done all these jobs, but it didn’t take long for ancient societies to realise that it was more efficient for people to specialise in one type of job and to offer the product of that job as a service. Two hundred thousand years later we have McDonald’s and Gap. Whether this is progress is for you to decide – but it is undeniable that this model has allowed vastly complex systems of millions of components (us) to function and thrive.

The service concept has taken application development by storm in recent years. Marketing teams have realised that tagging ‘As A Service’ to your product implies efficiency and productivity. Bugs ‘As A Service’ has arrived. The Service revolution has seen Processes migrate from isolated rural machines to large Data Centre Cities, where they coexist and provide their wares as Web Services.

So whey stop at the Process Level? Just as the service orientation of civilization progressed from the factory to the home with introduction of domestic appliances, developers are now becoming accustomed to the concept of ‘In-Process’ or ‘Micro’ Services.

After all, an application is just a sequence of jobs, and often the same job is done by many different parts of the application. Obtaining a data base connection is one such example. Many will not remember code where every class that needed a connection would ‘execute the job’ of creating it’s own connection – probably with a utility method, JDBCUtils.createConnection() or equivalent – but essentially still a lot of individual jobs. Thanks in a large part to Spring’s data source injection, we now take connections ‘as a service’ for granted.

If it is not made this easy though, prehistoric forces quickly lure developers back into developing monolithic classes that do every job. Burger Restaurant Classes sell Clothes  because they’ve already done the work of acquiring a mammoth.

Oddjob’s and Services

Now for the few of you that read my blogs regularly (hi mum, hi dad) – you are probably thinking that I’ve written an awful lot without mentioning Oddjob. Well – I think it’s time to mention that Oddjob is all about jobs and services.

Any class with a start() and stop() method is a service in Oddjob. Here is a convoluted Time Service:

public class TimeService implements Clock {

	private volatile ScheduledExecutorService executor;

	private volatile String cachedTime;

	public void start() {

		Runnable getTimeJob = new Runnable() {
			@Override
			public void run() {
				cachedTime = new SimpleDateFormat(
						"HH:mm:ss").format(new Date());
			}
		};
		getTimeJob.run();

		executor = Executors.newSingleThreadScheduledExecutor();

		executor.scheduleAtFixedRate(getTimeJob,
				0, 10, TimeUnit.SECONDS);
	}

	@Override
	public String getTime() {
		if (executor == null) {
			throw new IllegalStateException("Time Service unavailable");
		}
		return cachedTime;
	}

	public void stop() {
		executor.shutdown();
		executor = null;
	}
}

Once our service is started it calculates the time as a String every 10 seconds and caches the result.

Now plese don’t think that caching the date like this is a good idea – it would take squillions of calls to getTime() before any performance gains are worth the added complexity. This code is for demonstration purposes only!

Note also that the TimeService implements an interface Clock. This is not a requirement of Oddjob – this is just me – I can’t write a Service that doesn’t implement an interface. It isn’t right! For the record – here is Clock

public interface Clock {

	public String getTime();
}

Here is Oddjob starting and using our service to tell the time:

Time Service StartedI’ve re-run the Echo job a few times so we can see that time is updated. Here’s the Oddjob configuration:

<oddjob>
  <job>
    <sequential>
      <jobs>
        <bean class="oddjob.demos.TimeService" id="time-service" />
        <echo><![CDATA[The time is ${time-service.time}]]></echo>
      </jobs>
    </sequential>
  </job>
</oddjob>

The Time Service and Echo Job are both nested in a Sequential Job. The Sequential job will run any number child jobs in order. Is a service a job? No – but the act of starting a service is a job. Oddjob wraps starting a service up as a Runnable.

Just as for our mammoth hunting ancestors, life is still just a sequence of jobs.

 

Comments are closed.