Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

直连模式参数回调功能抛异常 #1472

Closed
cyfonly opened this issue Mar 15, 2018 · 5 comments
Closed

直连模式参数回调功能抛异常 #1472

cyfonly opened this issue Mar 15, 2018 · 5 comments

Comments

@cyfonly
Copy link

cyfonly commented Mar 15, 2018

直连模式下,使用参数回调功能Consumer端会抛出以下异常:
Caused by: java.lang.IllegalStateException: Serialized class com.dubbo.callback.Consumer$1 must implement java.io.Serializable

@leiwei2094
Copy link
Contributor

Missing implements java.io.Serializable?

@cyfonly
Copy link
Author

cyfonly commented Mar 30, 2018

public interface CallbackListener{
    void change(String msg);
}
public interface CallbackService {
    void addListener(String key, CallbackListener listener);
}
public class CallbackServiceImpl implements CallbackService{
    @Override
    public void addListener(String key, CallbackListener listener) {
        System.out.println("-------------Provider receive: key=" + key);
        listener.change("hehehehehe");
    }
}
public class Provider {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:callback_provider.xml" });
        context.start();
        System.in.read();
    }
}
public class Consumer {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:callback_consumer.xml");
        context.start();
        CallbackService callbackService = (CallbackService) context.getBean("callbackService");
        callbackService.addListener("Chenng", new CallbackListener() {
            @Override
            public void change(String msg) {
                System.out.println("============Consumer reveive:msg=" + msg);
            }
        });
    }
}

consumer.xml

   <dubbo:registry register="false"/>
   <dubbo:protocol name="dubbo" port="20880"/>
   <bean id="callbackService" class="com.dubbo.callback.CallbackServiceImpl" />
   <dubbo:service interface="com.dubbo.callback.CallbackService" ref="callbackService">
       <dubbo:method name="addListener">
           <dubbo:argument index="1" callback="true" type="com.dubbo.callback.CallbackListener"/>
       </dubbo:method>
   </dubbo:service>

provider.xml
<dubbo:reference id="callbackService" interface="com.dubbo.callback.CallbackService" url="dubbo://127.0.0.1:20880"/>

@chickenlj
Copy link
Contributor

Caused by: java.lang.IllegalStateException: Serialized class com.dubbo.callback.Consumer$1 must implement java.io.Serializable

Callback class should never serialized and transfered to provider, it's only a signal for internal implementation to do some trick. You may have missed some configurations.

@chickenlj
Copy link
Contributor

Just noticed you use peer to peer to invoke provider directly. I guess you missed some parameters for the target provider url.

@aqqwiyth
Copy link

aqqwiyth commented Apr 9, 2019

直连的模式下客户端无法感知服务端是否开启了callback.
而使用注册中心的情况下服务端会把方法的callback写入到注册中心里面去, 客户端也会根据参数做自适应

所以直连模式下添加下面的参数可以实现

<dubbo:reference url="dubbo://127.0.0.1:20880/?addListener.1.callback=true" .....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants