INT-718, OSGi-enabled Cafe sample. README provided
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.integration.samples.cafe.xml;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.context.support.FileSystemXmlApplicationContext;
|
||||
@@ -28,27 +29,34 @@ import org.springframework.integration.samples.cafe.Order;
|
||||
* order is placed, the Cafe will send that order to the "orders" channel.
|
||||
* The relevant components are defined within the configuration file
|
||||
* ("cafeDemo.xml").
|
||||
* If deployed in SpringSource dmServer the ApplicationContext configuration is in
|
||||
* META-INF/spring directory
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Marius Bogoevici
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class CafeDemo {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AbstractApplicationContext context = null;
|
||||
CafeDemo demo = new CafeDemo();
|
||||
AbstractApplicationContext applicationContext = null;
|
||||
if(args.length > 0) {
|
||||
context = new FileSystemXmlApplicationContext(args);
|
||||
applicationContext = new FileSystemXmlApplicationContext(args);
|
||||
}
|
||||
else {
|
||||
context = new ClassPathXmlApplicationContext("cafeDemo.xml", CafeDemo.class);
|
||||
applicationContext = new ClassPathXmlApplicationContext("cafeDemo.xml", CafeDemo.class);
|
||||
}
|
||||
Cafe cafe = (Cafe) context.getBean("cafe");
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
demo.performDemo(applicationContext, 100);
|
||||
}
|
||||
|
||||
public void performDemo(ApplicationContext applicationContext, int iterations){
|
||||
Cafe cafe = (Cafe) applicationContext.getBean("cafe");
|
||||
for (int i = 1; i <= iterations; i++) {
|
||||
Order order = new Order(i);
|
||||
order.addItem(DrinkType.LATTE, 2, false);
|
||||
order.addItem(DrinkType.MOCHA, 3, true);
|
||||
cafe.placeOrder(order);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.integration.samples.osgi.cafe;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.Constants;
|
||||
import org.osgi.framework.ServiceEvent;
|
||||
import org.osgi.framework.ServiceListener;
|
||||
import org.osgi.framework.ServiceReference;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.integration.samples.cafe.xml.CafeDemo;
|
||||
|
||||
/**
|
||||
* Simple BundleActivator which will register ServiceListener which will listen for
|
||||
* ApplicationContext published event. Once event is received, CafeDemo will be executed.
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class CafeBundleActivator implements BundleActivator, ServiceListener{
|
||||
private BundleContext context;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void start(BundleContext context) throws Exception {
|
||||
this.context = context;
|
||||
context.addServiceListener(this);
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void stop(BundleContext context) throws Exception {}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void serviceChanged(ServiceEvent serviceEvent) {
|
||||
ServiceReference sr = serviceEvent.getServiceReference();
|
||||
if (context.getBundle().getSymbolicName().equals(sr.getProperty(Constants.BUNDLE_SYMBOLICNAME))){
|
||||
final ApplicationContext applicationContext = (ApplicationContext) context.getService(sr);
|
||||
ExecutorService executor = Executors.newCachedThreadPool();
|
||||
executor.execute(new Runnable() {
|
||||
public void run() {
|
||||
new CafeDemo().performDemo(applicationContext, 20);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user