Coder Social home page Coder Social logo

Comments (9)

jhalterman avatar jhalterman commented on July 17, 2024

I've never actually used the RpcClient so I'm not surprised that it's not supported. Just looking at the code real quick though, it looks like an internal reference to the consumer is wiped when the channel shutdown occurs. You may need to configure ModelMapper to not recover the consumer:

confit.withConsumerRecovery(false);

...instead letting the Rpc client re-create the consumer after the channel is recovered:

config.withChannelListeners(new AbstractChannelListener() {
    public void onRecovery(Channel channel) {
        rpcClient.setupConsumer();
    }
});

I haven't tested this myself (busy on other things ATM), but let me know how it goes.

from lyra.

chaopeng avatar chaopeng commented on July 17, 2024

My fail... lyra can recovery rpc automatically. The exception because rabbitmq restart...

from lyra.

jhalterman avatar jhalterman commented on July 17, 2024

Sure, but even after a RabbitMQ restart hopefully you can reuse your RpcClient. Did that approach I pasted above work?

from lyra.

chaopeng avatar chaopeng commented on July 17, 2024

if it is restart, should re-create server and client on onRecovery() ....

from lyra.

jhalterman avatar jhalterman commented on July 17, 2024

On recovery will be called after the connection and channel have been recovered. This is where you'll need to tell the client to recover the consumer since RpcClient needs that to be done internally.

from lyra.

chaopeng avatar chaopeng commented on July 17, 2024

it works now, thank you for your help.

from lyra.

jhalterman avatar jhalterman commented on July 17, 2024

Did you end up using the channel listener approach?

from lyra.

chaopeng avatar chaopeng commented on July 17, 2024
public class RecoveryRabbitMQRpcServer {

    private RpcServer rpcServer;
    private ExecutorService es = Executors.newSingleThreadExecutor();

    public RecoveryRabbitMQRpcServer(String host, String vhost, String userName, String password, final String queue, final Class<? extends RpcServer> serverCls) throws IOException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Config config = new Config()
                .withRecoveryPolicy(new RecoveryPolicy()
                        .withMaxAttempts(20)
                        .withInterval(Duration.seconds(1))
                        .withMaxDuration(Duration.minutes(5)));

        config.withConsumerRecovery(false);
        config.withChannelListeners(new DefaultChannelListener() {
            @Override
            public void onRecovery(Channel channel) {
                try {
                    rpcServer = serverCls.getConstructor(new Class[]{Channel.class, String.class}).newInstance(channel, queue);
                    es.submit(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                rpcServer.mainloop();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        ConnectionOptions options = new ConnectionOptions().withHost(host).withVirtualHost(vhost).withUsername(userName).withPassword(password);
        ConfigurableConnection connection = Connections.create(options, config);

        Channel channel = connection.createChannel();

        channel.queueDeclare(queue, false, false, false, null);

        this.rpcServer = serverCls.getConstructor(new Class[]{Channel.class, String.class}).newInstance(channel, queue);
        es.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    rpcServer.mainloop();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

}
public class RecoveryRabbitMQRpcClient {

    private Channel channel;
    private RpcClient rpcClient;

    /**
     *
     * @param timeout -1==NO_TIMEOUT
     */
    public RecoveryRabbitMQRpcClient(String host, String vhost, String userName, String password, final String queue, final int timeout) throws IOException {
        Config config = new Config()
                .withRecoveryPolicy(new RecoveryPolicy()
                        .withMaxAttempts(20)
                        .withInterval(Duration.seconds(1))
                        .withMaxDuration(Duration.minutes(5)));

        config.withConsumerRecovery(false);
        config.withChannelListeners(new DefaultChannelListener() {
            @Override
            public void onRecovery(Channel channel) {
                super.onRecovery(channel);
                try {
                    rpcClient = new RpcClient(channel, "", queue, timeout);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        ConnectionOptions options = new ConnectionOptions().withHost(host).withVirtualHost(vhost).withUsername(userName).withPassword(password);
        ConfigurableConnection connection = Connections.create(options, config);

        this.channel = connection.createChannel();


        this.rpcClient = new RpcClient(channel, "", queue, timeout);
    }

    public String stringCall(String s) throws IOException, TimeoutException {
        return this.rpcClient.stringCall(s);
    }

}

from lyra.

jhalterman avatar jhalterman commented on July 17, 2024

Thanks. I actually compiled the approach I was trying to get at and posted it here:

https://github.com/jhalterman/lyra/wiki/Lyra-Cookbook#recoverable-rpcclient

from lyra.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.