Coder Social home page Coder Social logo

Comments (5)

Allan-Nava avatar Allan-Nava commented on July 18, 2024

I used Lyra library but when the client lost connection, it still create a new connections.

This is my subiscribe method:

`

private void subscribe(final Handler handler)
{
    subscribeThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while(true) {
                try {
                    //connection = connectionFactory.newConnection();
                    connection = MySingletonConnection.getInstance().getConnection();
                    //connection = Connections.create(options, config);
                    Channel channel = connection.createChannel();
                    // Declare a queue and bind it to an exchange.
                    com.rabbitmq.client.AMQP.Queue.DeclareOk q = channel.queueDeclare("manager2box-"+ROUTING_KEY, true, false, true, null);
                    channel.queueBind(q.getQueue(), EXCHANGE, ROUTING_KEY);
                    // Create the QueueingConsumer and have it consume from the queue
                    QueueingConsumer consumer = new QueueingConsumer(channel);
                    channel.basicConsume(q.getQueue(), false, consumer);

                    while (true) {
                        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                        String message = new String(delivery.getBody());
                        Log.d(TAG, message);
                        Message msg = handler.obtainMessage();
                        Bundle bundle = new Bundle();
                        bundle.putString("commandBundle", message);
                        msg.setData(bundle);
                        handler.sendMessage(msg);
                    }
                } catch (InterruptedException e) {
                    Log.d(TAG, "InterruptedException");
                    e.printStackTrace();
                    break;
                } catch (Exception e1) {
                    Log.d(TAG, "Connection broken: " + e1.getClass().getName());
                    e1.printStackTrace();
                    try {
                        Thread.sleep(5000); //sleep and then try again
                    } catch (InterruptedException e) {
                        Log.d(TAG, "InterruptedException");
                        e.printStackTrace();
                        break;
                    }
                }
            }
        }
    });
    subscribeThread.start();
}

This is my SingletonConnection:

public class MySingletonConnection{
    public static final MySingletonConnection INSTANCE = new MySingletonConnection();
    private static Connection connection;

    private final String TAG = "MySingletonConnection";
    private final String EXCHANGE    = "manager2box";
    private final String USERNAME    = "manager";
    private final String PASSWORD    = "MGXxXq72HscXzakR";
    //private final String HOST        = "192.168.1.147";
    private final String HOST        = "portal.seafy.me";
    private final String ROUTING_KEY = Utils.getIPAddress();
    private Config config;
    private ConnectionOptions options;

    private MySingletonConnection(){
        //Lyra config
        config = new Config()
                .withConnectionListeners()
                .withRecoveryPolicy(RecoveryPolicies.recoverAlways())
                .withRetryPolicy(new RetryPolicy()
                        .withMaxAttempts(20)
                        .withInterval(Duration.seconds(5))
                        .withMaxDuration(Duration.minutes(5)));
        //Lyra option config for connection
        options = new ConnectionOptions().withUsername(USERNAME)
                .withRequestedHeartbeat(Duration.seconds(30))
                .withPassword(PASSWORD)
                .withHost(HOST)
                .withPort(5672);
        try {
            connection = Connections.create(options, config);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }

    public static MySingletonConnection getInstance(){
        return INSTANCE;
    }

    public Connection getConnection( ) {
        return connection;
    }
}
```
`

from lyra.

michaelklishin avatar michaelklishin commented on July 18, 2024

Are you sure that opening new connections and channels in a while(true) loop is really necessary?
Any client with that kind of code would produce a connection storm.

from lyra.

michaelklishin avatar michaelklishin commented on July 18, 2024

This should be closed as it has nothing to do with Lyra. The same question was answered in a RabbitMQ Java client issue and on rabbitmq-users.

from lyra.

Allan-Nava avatar Allan-Nava commented on July 18, 2024

from lyra.

michaelklishin avatar michaelklishin commented on July 18, 2024

Duplicate of #75, addressed by #82.

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.