- 浏览: 546285 次
- 性别:
- 来自: 西安
文章分类
- 全部博客 (251)
- UML/RUP/软件工程 (0)
- DDD:领域驱动设计 (1)
- IaaS/Paas/SaaS (1)
- Hadoop/YARN (10)
- PBDP项目 (2)
- OSGi-基础 (7)
- OSGi-Aries (2)
- OSGi-SpringDM (32)
- Oracle/MySQL/SS (8)
- Felix/Equinox/Bnd (14)
- Virgo Server/Gemini (7)
- Weblogic/JBoss/Tomcat (10)
- ActiveMQ (14)
- Camel (1)
- Spring Roo/Eclipse (7)
- Java/JSP/JSF (10)
- Maven (19)
- ESB-Mule (1)
- ESB-ServiceMix (18)
- SOA/SCA/SDO (12)
- WebService/RESTful (17)
- JS/jQuery/ExtJS (4)
- Spring/JPA/MVC (15)
- SpringSecurity (5)
- BPM/jBPM (3)
- Hudson/SVN/CI (0)
- LinuxOS/虚拟化 (25)
- Office/OpenOffice (1)
- 项目管理 (5)
- POI/jFreeChart (1)
最新评论
-
panamera:
请问JMS Transport 发布的webservice 是 ...
CXF 提供的Service Transport-JMS Transpor -
jianyi296:
where is attachment.
CXF WebService Dynamic Client -
hj01kkk:
您好,我用jdbc-user-service为什么每次验证时都 ...
SpringSecurity笔记3-Authenticating Users:Authenticaton Strategy -
wufenglin1231:
你好,我在实现Mapping exceptions to re ...
RESTful 异常处理 -
xchd:
[echo] Project: common
[echo ...
Hive安装配置学习笔记
1. Bundles
获取Bundle信息接口:BundleContext,方法:
getBundles();获取当前容器中所有的Bundle
getBundle(long bundleId);获取指定Id的Bundle
getBundle();获取的当前Bundle
例如:
BundleContext bundleContext = (...)
Bundle[] bundles = bundleContext.getBundles();
long bundleIdentifier = 12;
Bundle bundle = bundleContext.getBundle(
bundleIdentifier
);
Bundle currentBundle = bundleContext.getBundle();
Accessing general and configuration information about a bundle:
Bundle bundle = (...)
Long bundleId = bundle.getBundleId();
String location = bundle.getLocation();
String symbolicName = bundle.getSymbolicName();
Dictionary<String,String> headers = bundleContext.getBundle().getHeaders();
String bundleName = headers.get(Constants.BUNDLE_NAME);
String bundleVersion = headers.get(Constants.BUNDLE_VERSION);
getResource()和findEntries()方法:
Bundle bundle = (...);
URL specificConfigurationFile = bundle.getResource("META-INF/spring/osgi-context.xml");
Enumeration configurationFiles = bundle.findEntries("META-INF/spring", "*.xml", false);
2. Lifecycle Management
States in a bundle’s lifecycle:
Installed: Bundle is installed within the OSGi container.
Uninstalled: Bundle is uninstalled from the OSGi container.
Resolved: Bundle is installed and all dependencies are resolved.
Starting: Bundle is starting, moving from the resolved to the active state.
Active: Bundle is available to do work within the OSGi container.
Stopping: Bundle is stopping, moving from the active to the resolved state.
例如:
BundleContext bundleContext = (...);
String absoluteBundlePath = (...);
Bundle bundle = bundleContext.installBundle(absoluteBundlePath);
int state = bundle.getState();
bundle.start();
int state = bundle.getState();
3. Properties
Access global properties which are defined by the OSGi Container via the BundleContext’s
getProperty method。
Several property keys are standardized in the OSGi specification and are present in the Constants
interface—these constants can be used as parameters of the getProperty method:
■ Constants.FRAMEWORK_VERSION and Constants.FRAMEWORK_VENDOR—The version
and name of the OSGi container used
■ Constants.FRAMEWORK_LANGUAGE—The language used by the container to implement
components
■ Constants.FRAMEWORK_OS_NAME and Constants.FRAMEWORK_OS_VERSION—The name and
version of the operating system that’s running the container
■ Constants.FRAMEWORK_PROCESSOR—The name of the processor corresponding to the host
computer
例如:
String containerName = bundleContext.getProperty(Constants.FRAMEWORK_VENDOR);
其中:如果在OSGi容器中搜索不到指定的属性,将搜索JVM的系统属性。
4. Event Support
OSGi’s event support at the bundle level allows code to be notified of every update to the state
of a component during its lifetime. It provides two ways to handle these events:
■ An asynchronous method using the BundleListener interface
■ A synchronous method using the SynchronousBundleLister interface
其中:The SynchronousBundleListener interface extends BundleListener without adding anything
more.
Handling a bundle event in an asynchronous listener:
public class SimpleBundleListener implements BundleListener {
public void bundleChanged(BundleEvent event) {
int type = event.getType();
String symbolicName = event.getBundle().getSymbolicName();
if (type==BundleEvent.STARTED) {
System.out.println("Bundle "+symbolicName+" started...");
} else if (type==BundleEvent.STOPPED)
System.out.println("Bundle "+symbolicName+" stopped...");
}
}
}
注册该监听器:使用BundleContext的addBundleListener方法。
5. Persistent Storage Area
Interacting with the persistent storage area of a component:
BundleContext bundleContext = (...)
File directory = bundleContext.getDataFile("/data");
if (!directory.exists()) {
directory.mkdir();
}
File file = bundleContext.getDataFile("/data/test.txt");
FileWriter writer = null;
try {
writer = new FileWriter(file, true);
writer.write("my content");
} catch(IOException ex) {
(...)
} finally {
closeWriter(writer);
}
6. BundleActivator
Registering and unregistering services with a BundleActivator:
public class SimpleActivator implements BundleActivator {
(...)
public void start(BundleContext context) {
registerServices(context);
}
public void stop(BundleContext context) {
unregisterServices(context);
}
}
其中该Activator 需要在MAINFEST.MF文件中通过Bundle-Activator Header定义。
7. Service support in OSGi
The OSGi services feature is based on the service registry, which provides nameand property
-based lookup, registration, and management of all service instances registered in the container.
It also can be used to notify components when changes occur, to prevent errors from occurring.
7.1 Providing Service
使用BundleContext的registerService()方法,该方法返回ServiceRegistration,该方法有 一下参数:
(1) clazz or clazzes: 一个或几个名字用来注册和 识别服务。 在OSGi中,这些名称对应的是 服务类的名称,
或者,如果是基于接口的编程,则是 实现的接口的名称 。 类型检查时将使用这些名字 。 具有相同名称的多哥服
务可共存于OSGi服务注册表 。
(2) service:The implementation of the service itself, or an instance of the ServiceFactory interface.
(3) properties: A set of properties to associate with the service. These properties must be of
type String. The value can be null if there are no properties. And the OSGi container automatically
inserts two additional properties that are related to the service identifier (service.id) and the
classes used for the registration (objectClass).
Simple implementation of the HelloWorldService interface:
public class SimpleServiceActivator implements BundleActivator {
private ServiceRegistration serviceRegistration;
public void start(BundleContext bundleContext) throws Exception {
String serviceName = "com.manning.sdmia.osgi.services.providing.service.SimpleService";
SimpleServiceImpl service = new SimpleServiceImpl();
Properties properties = new Properties();
properties.setProperty("creationDate",(new Date()).toString());
this.serviceRegistration= bundleContext.registerService(serviceName, service,
serviceProperties);
}
public void stop(BundleContext bundleContext) throws Exception {
if (this.serviceRegistration!=null) {
this.serviceRegistration.unregister();
}
}
}
Simple implementation of the ServiceFactory interface:
public class SimpleServiceFactory implements ServiceFactory {
public Object getService(
Bundle bundle, ServiceRegistration registration) {
String bundleSymbolicName = bundle.getSymbolicName();
if (bundleSymbolicName.startsWith("com.manning")) {
return new SimpleForManningServiceImpl();
} else {
return new SimpleServiceImpl();
}
}
public void ungetService(Bundle bundle,
ServiceRegistration registration, Object service) {
}
}
Note that implementations of this interface are used by the container and therefore must be
thread-safe. The instance returned by the getService method is cached by the framework until
the bundle releases the service.
7.2 Using Service
(1) Basic Approach
1. Get a ServiceReference instance for the service, based on the service name from the BundleContext instance.
2. Get the service instance itself, based on the BundleContext instance and the previously obtained ServiceReference instance.
Consuming an OSGi service:
String serviceName = SimpleService.class.getName();
ServiceReference serviceReference = null;
try {
serviceReference = bundleContext.getServiceReference(serviceName);
if (serviceReference!=null) {
SimpleService service= (SimpleService)bundleContext.getService(serviceReference);
service.test();
}
} catch(Exception ex) {
(...)
} finally {
if (serviceReference!=null) {
bundleContext.ungetService(serviceReference);
}
}
其中:ServiceReference instances can also be obtained by using bundle instances, which provide
the following methods:
■ getRegisteredServices—Returns the list of services registered by the bundle
■ getServicesInUse—Returns the list of the bundle services used by other bundles
(2) Service Tracker
需现在MANIFEST.MF中Import-Package:org.osgi.util.tracker,Using the ServiceTracker:
String serviceName = SimpleService.class.getName();
ServiceTracker serviceTracker = null;
try {
serviceTracker = new ServiceTracker(
bundleContext, serviceName, null);
serviceTracker.open();
SimpleService service = (SimpleService) serviceTracker.getService();
service.test();
} catch(Exception ex) {
(...)
} finally {
if (serviceTracker!=null) {
serviceTracker.close();
}
}
7.3 Service Event Support
使用ServiceLinstner接口,A ServiceListener implementation handling service events:
public class SimpleServiceListener implements ServiceListener {
private String getServiceName(ServiceEvent event) {
String[] objectClass = (String[])event.getServiceReference().getProperty("objectClass");
return objectClass[0];
}
public void serviceChanged(ServiceEvent event) {
int type = event.getType();
String serviceName = getServiceName(event);
String symbolicName = event.getServiceReference().getBundle().getSymbolicName();
if (type==ServiceEvent.REGISTERED) {
System.out.println("Service " + serviceName + " registred by the bundle "
+ symbolicName + "...");
} else if (type==ServiceEvent.UNREGISTERING) {
System.out.println("Service " + serviceName + " being unregistered by the bundle "
+ symbolicName + "...");
} else if (type==ServiceEvent.MODIFIED) {
System.out.println("Service " + serviceName + " modified by the bundle "
+ symbolicName + "...");
}
}
}
注册该监听器:
BundleContext context = (...)
SimpleServiceListener listener = new SimpleServiceListener();
Context.adServiceListener(listener);
发表评论
-
OSGi网络资料搜集
2012-05-11 16:25 01. 用OSGi应用程序开发和工作的最佳实践 ... -
OSGi Best Practice:Maven Bundle Plugin
2012-02-01 16:44 33651. OSGi Tooling (1) Use th ... -
OSGi MANIFEST.MF文件
2011-09-04 10:14 2590A bundle is a group of Java ... -
OSGi 对Native Code的支持
2011-08-13 11:34 1293OSGi provides support for ... -
OSGi组件依赖
2011-08-12 10:56 1628OSGi doesn’t allow you to s ... -
OSGi类加载机制
2011-08-12 10:40 20930. Java’s Virtual Machine, Java ... -
OSGi Concepts and Architecture
2011-08-10 11:36 708JSR-277 :Java Module System ...
相关推荐
在任何使用 Felix GoGo 命令的 OSGi 容器内交互; 它由以下组件组成: jbehave-osgi-bundles/org.jbehave.osgi.core。 包装 JBehave 核心 API 并公开其功能的包; 这是所有情况下的必需捆绑包。 jbehave-osgi-...
还有OSGI功能,可将组件部署在OSGI容器上,例如Fuse 6.2 该组件支持以下操作 流程操作: START_PROCESS,ABORT_PROCESS_INSTANCE,SIGNAL_EVENT,GET_PROCESS_INSTANCE,GET_PROCESS_INSTANCES 规则运算: FIRE_...
现在类加载器在 Web 容器和 OSGi 中得到了广泛的使用。一般来说,Java 应用的开发人员不需要直接同类加载器进行交互。Java 虚拟机默认的行为就已经足够满足大多数情况的需求了。不过如果遇到了需要与类加载器进行...
现在类加载器在 Web 容器和 OSGi 中得到了广泛的使用。一般来说,Java 应用的开发人员不需要直接同类加载器进行交互。Java 虚拟机默认的行为就已经足够满足大多数情况的需求了。不过如果遇到了需要与类加载器进行...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
WebSocket4J 是一个用 Java 实现的 WebSocket 协议的类库,可使用 Java 来构建交互式 Web 应用。WebSocket4J 并未实现客户端通讯协议,所以不能用它来连接 WebSocket 服务器。 Struts验证码插件 JCaptcha4Struts2 ...
在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息的内容。同时一个mail note将被发送给消息发送者,发送一个e-mail通知给由recipient参数确定的e-...
在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息的内容。同时一个mail note将被发送给消息发送者,发送一个e-mail通知给由recipient参数确定的e-...