You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by "J.W. Janssen (JIRA)" <ji...@apache.org> on 2015/01/06 11:21:34 UTC
[jira] [Commented] (FELIX-4751) getAllServiceReferences() Throw
IllegalStateException: Invalid BundleContext
[ https://issues.apache.org/jira/browse/FELIX-4751?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14265935#comment-14265935 ]
J.W. Janssen commented on FELIX-4751:
-------------------------------------
Using {{BundleContext#getAllServiceReferences}} is most often not necessary: it returns all service references *even* those that are not usable/assignable in your bundle (see the [JavaDoc|http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleContext.html#getAllServiceReferences(java.lang.String, java.lang.String)]).
You probably want to use {{BundleContext#getServiceReferences}} instead, which does only return those services that your bundle can use.
> getAllServiceReferences() Throw IllegalStateException: Invalid BundleContext
> ----------------------------------------------------------------------------
>
> Key: FELIX-4751
> URL: https://issues.apache.org/jira/browse/FELIX-4751
> Project: Felix
> Issue Type: Bug
> Components: Framework
> Affects Versions: framework-4.0.1
> Environment: Centos 6.0 JDK 6
> Reporter: Chenqiulin
>
> I register service like this:
> {code}
> public void start(BundleContext context) throws Exception {
> Dictionary props = new Hashtable();
> // service.pid
> String symbolicName = (String) context.getBundle().getHeaders()
> .get("Bundle-SymbolicName");
> props.put("service.pid", symbolicName);
> props.put(Constant.COMMANDNAME, "manager");
>
> serviceRegistrationRuntime = context.registerService(
> AgentAction.class.getName(), new ManagerAction(context), props);
> logger.info("manager Service regist success.");
> }
> {code}
> and I get the service like this:
> {code}
> public Response handle(Message message) {
> ServiceReference[] serviceRefs = null;
> Response result = null;
> try {
> String mess = message.getContent().toString();
> logger.debug(mess);
> Map map = JsonUtil.toBean(mess, Map.class);
> logger.info("begin to handle message " + map.get(MODULENAME));
> serviceRefs = context.getAllServiceReferences(
> AgentAction.class.getName(), "(" + Constant.COMMANDNAME
> + "=" + map.get(MODULENAME) + ")");
> // serviceRefs = context.getAllServiceReferences(
> // AgentAction.class.getName(),null);
> if (serviceRefs.length == 0) {
> throw new Exception("has not command service like this");
> }
> if (serviceRefs.length > 1) {
> throw new Exception("more than one command service like this");
> }
> AgentAction action = (AgentAction) context
> .getService(serviceRefs[0]);
> // handle the command
> result = action.handle(message);
> } catch (Exception e) {
> result = message.createResponse();
> result.setErrorMsg( e.getMessage());
> logger.error(e.getMessage(), e);
> }
> return result;
> }
> {code}
> the phenomenon is I always catch the Exception(about 50%) like this:
> {code}
> ERROR - cn.tianya.amm.agent.service.AgentMessageHandler - Invalid BundleContext.
> java.lang.IllegalStateException: Invalid BundleContext.
> at org.apache.felix.framework.BundleContextImpl.checkValidity(BundleContextImpl.java:514)
> at org.apache.felix.framework.BundleContextImpl.getAllServiceReferences(BundleContextImpl.java:411)
> at cn.tianya.amm.agent.service.AgentMessageHandler.handle(AgentMessageHandler.java:55)
> at cn.tianya.fw.mq.impl.activemq.FwMqListenService$MessageSyncHandlerAdapter.run(FwMqListenService.java:582)
> at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> at java.lang.Thread.run(Thread.java:619)
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)