Skip to content

NullPointerException happened when using SpringContainer.getContext() #3476

Closed

Description

@stoneapple
  • Dubbo version: 2.5.9
  • Operating System version: Suse Linux 11
  • Java version: 1.8

Steps to reproduce this issue

  1. write a project that is a provider side exposed serviceA and in the service implimentation invokes another remote service serviceB. The invoke code is like this SpringContainer.getContext().getBean("ref");
  2. continious request from outside for the serviceA (many node for serviceA)
  3. stop one serviceA node
  4. start the stopped serviceA node
  5. almost every time first one or two serviceA invoke failed, exception is NullPointerException, occurred for SpringContainer.getContext();

Reason: dubbo service is exposed with spring container ContextRefreshedEvent。 but that moment SpringContainer.getContext() is not started, if it is ContextStartedEvent, this bug will be fixed.

Activity

  1. chickenlj commented on Feb 18, 2019

    @chickenlj
    Contributor
    1. write a project that is a provider side exposed serviceA and in the service implimentation invokes another remote service serviceB. The invoke code is like this SpringContainer.getContext().getBean("ref");

    I don't understand why SpringContainer.getContext().getBean("ref"); will throw NPE, by the time contextRefreshedEvent is send, the reference of ServiceB should be ready in ApplicationContext for use.

    context.start() should only be responsible for those beans that have implemented Lifecycle, as the javadoc showed:

    /**
    	 * Start all registered beans that implement {@link Lifecycle} and are <i>not</i>
    	 * already running. Any bean that implements {@link SmartLifecycle} will be
    	 * started within its 'phase', and all phases will be ordered from lowest to
    	 * highest value. All beans that do not implement {@link SmartLifecycle} will be
    	 * started in the default phase 0. A bean declared as a dependency of another bean
    	 * will be started before the dependent bean regardless of the declared phase.
    	 */
    	@Override
    	public void start() {
    		startBeans(false);
    		this.running = true;
    	}
  2. stoneapple commented on Feb 18, 2019

    @stoneapple
    ContributorAuthor
    1. write a project that is a provider side exposed serviceA and in the service implimentation invokes another remote service serviceB. The invoke code is like this SpringContainer.getContext().getBean("ref");

    I don't understand why SpringContainer.getContext().getBean("ref"); will throw NPE, by the time contextRefreshedEvent is send, the reference of ServiceB should be ready in ApplicationContext for use.

    context.start() should only be responsible for those beans that have implemented Lifecycle, as the javadoc showed:

    /**
    	 * Start all registered beans that implement {@link Lifecycle} and are <i>not</i>
    	 * already running. Any bean that implements {@link SmartLifecycle} will be
    	 * started within its 'phase', and all phases will be ordered from lowest to
    	 * highest value. All beans that do not implement {@link SmartLifecycle} will be
    	 * started in the default phase 0. A bean declared as a dependency of another bean
    	 * will be started before the dependent bean regardless of the declared phase.
    	 */
    	@Override
    	public void start() {
    		startBeans(false);
    		this.running = true;
    	}

    因为服务暴露是在Spring的ContextRefresh阶段,服务暴露后,就会有交易进来。这个阶段SpringContainer中的XMLClasspathApplicationContext对象还在启动中,并没有赋值成功,但是服务已经暴露了,如果交易量大的情况下,服务实现类里面通过SpringContainer.getContext().getBean("ref");去获取另外一个服务的引用,肯定会报NPE的,不知道说清楚了没。

  3. chickenlj commented on Feb 19, 2019

    @chickenlj
    Contributor

    多谢,明白问题了:
    SpringContainer.getContext()返回null
    我刚开始理解成SpringContainer.getContext().getBean("ref");找不到bean。

  4. added this to the 2.7.1 milestone on Feb 20, 2019
  5. stoneapple commented on Feb 21, 2019

    @stoneapple
    ContributorAuthor

    多谢,明白问题了:
    SpringContainer.getContext()返回null
    我刚开始理解成SpringContainer.getContext().getBean("ref");找不到bean。

    我们有个解决思路,把contextrefresh 事件换成contextstarted事件,这样的改法能解决这个问题,请你们看下是否有其他问题,这个修改方案,我提一个PR

  6. cvictory commented on Feb 22, 2019

    @cvictory
    Contributor
  7. stoneapple commented on Feb 25, 2019

    @stoneapple
    ContributorAuthor

    3 question: 1. Why not ust ApplicationContext.getBean to get a Bean directly? 2. I think exporting service when context refresh event is invoked is reasonable. 3. I think there are some problem when online or offline service . So you can refer : http://dubbo.apache.org/zh-cn/docs/user/demos/graceful-shutdown.html I donot know what I said can answer your question. If not , let me know. Longfei Xia [email protected] 于2019年2月21日周四 下午11:50写道:
    …
    多谢,明白问题了: SpringContainer.getContext()返回null 我刚开始理解成SpringContainer.getContext().getBean("ref");找不到bean。 我们有个解决思路,把contextrefresh 事件换成contextstarted事件,这样的改法能解决这个问题,请你们看下是否有其他问题,这个修改方案,我提一个PR — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#3476 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AD5mbN8dErjtIizaMi31nqEJ1jabbVqwks5vPrpdgaJpZM4a7Lal .
    -- Best Regard! cvitory

    在一个服务的具体实现类内部,我们是没法获取到ApplicationContext对象的,这个对象是main方法启动的时候才有,而且在启动过程中,它也是null,无法通过他来getbean

  8. beiwei30 commented on Mar 5, 2019

    @beiwei30
    Member

    在一个服务的具体实现类内部,我们是没法获取到ApplicationContext对象的,这个对象是main方法启动的时候才有,而且在启动过程中,它也是null,无法通过他来getbean

    我的意见是,这种用法不太对,service 实现类直接实现 ApplicationContextAware 自然而然就拿到 context 实例了。相反,我觉得 SpringContainer 上根本就不应该暴露 getContext 方法。

  9. mercyblitz commented on Mar 5, 2019

    @mercyblitz
    Contributor

    @stoneapple Did you start SpringContainer in concurrency? Why you have to get a nulling ClassPathXmlApplicationContext?

  10. beiwei30 commented on Mar 5, 2019

    @beiwei30
    Member

    otherwise we may change the original impl to:

        @Override
        public void start() {
            String configPath = ConfigUtils.getProperty(SPRING_CONFIG);
            if (StringUtils.isEmpty(configPath)) {
                configPath = DEFAULT_SPRING_CONFIG;
            }
            context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+"), false);
            context.refresh();
            context.start();
        }
  11. cvictory commented on Mar 5, 2019

    @cvictory
    Contributor

    除了本身优化外,使用优雅上线服务,也可以解决这个问题吧。
    整个spring还没启动好,被提早依赖其他bean。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions