[英]how useful is to create an osgi utility bundle
我正在尝试使用OSGi框架开发一个简单的应用程序。 我的问题涉及框架中可用的“实用程序包”:让我用一个非常详细的示例进行解释。 目前,我正在尝试建立一个捆绑发送的事件。
据我了解,我需要做如下事情( event admin felix ):
public void reportGenerated(Report report, BundleContext context)
{
ServiceReference ref = context.getServiceReference(EventAdmin.class.getName());
if (ref != null)
{
EventAdmin eventAdmin = (EventAdmin) context.getService(ref);
Dictionary properties = new Hashtable();
properties.put("title", report.getTitle());
properties.put("path" , report.getAbsolutePath());
properties.put("time", System.currentTimeMillis());
Event reportGeneratedEvent = new Event("com/acme/reportgenerator/GENERATED", properties);
eventAdmin.sendEvent(reportGeneratedEvent);
}
}
现在,由于OSGi应用程序可能有很多捆绑包,因此我想为每个捆绑包创建Event的子类(例如,我有一个名为“ BundleExample”的捆绑包?在其导出的类中将有一个“ BundleExampleEvent”)。 我知道这不会添加任何信息,因为您可以通过查看“主题”来了解收到的事件,但是请耐心等待 。
现在, Event
构造函数需要一个主题和一个Map<String, Object>
。 但是,为了“简化”事件构造函数,我只想将主题和参数列表放入映射中。 例如,这可能是BundleExampleEvent类:
public class BundleExampleEvent extends Event{
private int importantVariable;
public BundleExampleEvent(String topic, int importantVariable) {
super(topic, Utils.toMap("importantVariable", importantVariable));
//here toMap is static
}
public int getImportantVariable() {
return this.importantVariable;
}
}
好的,请注意Utils.toMap
:这是一个允许您将String, Object
序列转换为Map
的函数。 好的,现在Utils
是实用程序类的一个示例(愚蠢,无用但仍然是实用程序类)。 本着OSGi的精神,我也希望使该实用程序类成为捆绑软件 :我的想法是在框架启动时启动此Utils捆绑软件,然后每当我需要其实用工具之一时,我都希望通过@Reference
批注获取引用。
可以在任何bundle接口实现中很好地工作,如下所示:
@Component
public class BundleExampleImpl implements BundleExample {
@Reference
private Utils utils;
@Override
public String sayHello() {
return this.utils.fetchHello();
//another useless utility function, but hopefully it conveys what i'm trying to do
}
}
但是其他类(例如BundleExampleImpl在其工作期间调用)又如何呢? 例如, BundleExampleEvent
怎么BundleExampleEvent
? 我需要从sayHello
方法调用它,并且我想在该类中也使用此实用程序来计算Map! 在前面的示例中,我使用了静态函数,但是我想使用Utils
OSGi给我的引用。
当然,我可以在BundleExampleEvent
的构造函数中添加一个参数以满足链接的要求,但我不想这样做,因为有些东西依赖于“实用程序类”,这很愚蠢。 我的问题是:
或者我可以做一些奇怪的事情,比如在BundleExampleEvent
添加Utils的引用; 即这样的事情:
public class BundleExampleEvent extends Event{ @Reference private Utils utils; private int importantVariable; public BundleExampleEvent(String topic, int importantVariable) { super(topic, Utils.toMap("importantVariable", importantVariable)); //here toMap is static } public int getImportantVariable() { return this.importantVariable; } }
也许拥有“公用事业捆绑包”的整个想法只是纯粹的垃圾?
感谢您的任何答复。 希望我能以最清晰的方式传达我的问题
我认为Utils作为服务没有任何意义。 如果事物可以想象有多个实现,那么它们应该仅是一种服务。 在您的情况下,Util功能的使用者只需要一个实现...实现就是合同。
我什至不认为utils代码应该捆绑在一起。 只需将其放入静态链接到需要它的包的库中即可。
在您的情况下,Utils utils将是OSGi服务。 然后,您想在不是BundleExampleEvent之类的服务的对象内部使用此服务。
您可以做的是创建一个服务,该服务创建BundleExampleEvent实例并将其提供给OSGi服务。 有点像工厂服务。 问题在于OSGi中的服务是动态的。 如果BundleExampleEvent实例所需的服务消失,则必须丢弃该对象。 因此,这仅适用于短暂的对象。
在eventadmin示例中,另一种解决方案是不使用特殊事件类,而是创建一个具有发送此类事件的方法的服务。 然后,所有魔术将在此方法内发生,结果将是没有进一步逻辑的事件。 您也可以使用DS将EventAdmin注入该服务。 这在OSGI中效果很好,但缺点是贫血域模型( http://www.martinfowler.com/bliki/AnemicDomainModel.html )。
我不确定要选择哪种变体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.