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:16:34 UTC

[jira] [Updated] (FELIX-4751) getAllServiceReferences() Throw IllegalStateException: Invalid BundleContext

     [ https://issues.apache.org/jira/browse/FELIX-4751?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

J.W. Janssen updated FELIX-4751:
--------------------------------
    Description: 
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}

  was:
I register service like this:

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.");
	}

and I get the service like this:
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;
	}


the phenomenon is  I always catch the Exception(about 50%) like this:

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)



> 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)