如何在android中管理RabbitMq的连接和通道

时间:2015-09-12 11:15:53

标签: java android rabbitmq

我正在使用RabbitMq开发一个Android通知服务。它工作正常,但有一个问题。我的通知服务每隔30秒打开一个连接,收到消息后,它将关闭通道和连接。确切地说我的问题在这里!如果有两个用户最多使用我的应用程序应该有两个连接和两个通道!但我测试了我的应用程序,大约有45个连接&渠道开放。我不知道我的代码有什么问题!这是我的代码:

     //------- start service every 30 sec on--------------------


        Intent ishintent = new Intent(MainActivity.this, NotificationService.class);
        PendingIntent pintent = PendingIntent.getService(MainActivity.this, 0, ishintent, 0);
        AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pintent);
        alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 30000, 30000, pintent);



        //---------------------- END SERVICE----------------------------  
    public class NotificationService extends Service
{

    ConnectionFactory factory = new ConnectionFactory();
    Connection connection;
    Channel channel;
    SharedPreferences sp1;
    String unm;
    String HostName = "my IP";
    String UserName = "user";
    String Password = "pass";

    @Override
    public void onCreate()
    {


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {

        sp1=getSharedPreferences("Login", 0);
        unm=sp1.getString("Usr", null);
        String qName=unm;
        String routeKey=unm;
        if( isOnline()) {

            new AsyncSubscribe().execute(qName, routeKey);


                      }

        return Service.START_STICKY;
    }


 // close connection when service finish 
    @Override
    public void onDestroy() {
        if (connection.isOpen()) {

            try {
                channel.close();
                connection.close();

            } catch (IOException e) {

                Log.e("MYAPP", "exception", e);

            } catch (TimeoutException e) {

                Log.e("MYAPP", "exception", e);
            }


        }

    }



    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

// set connection and channels
    public void SubscribeSet() {


        try {
            factory.setHost(HostName);
            factory.setUsername(UserName);
            factory.setPassword(Password);
            connection = factory.newConnection();
            channel = connection.createChannel();
        }
        catch ( Exception e)
        {
            Log.e("MYAPP", "exception", e);
        }

    }


    private class AsyncSubscribe extends AsyncTask<String, String, Void> {
        Consumer consumer;
        boolean flag=true;
               @Override
        protected void onProgressUpdate(String ... params) {
            super.onProgressUpdate(params);

                     // do something

        }

             @Override
                 protected Void doInBackground(String... params) {

                 SubscribeSet();

                  try {

                    channel.exchangeDeclare("logs", "fanout", true);
                    channel.exchangeDeclare("direct_logs", "direct", true);
                    channel.queueDeclare(params[0], true, false, false, null);
                    channel.queueBind(params[0], "logs", "");
                    channel.queueBind(params[0], "direct_logs", params[1]);

                    consumer = new DefaultConsumer(channel) {
                        @Override
                        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
                        throws IOException {
                            properties.builder().deliveryMode(2);
                            publishProgress(new String(body, "UTF-8"));

                        }
                    };
                      // listen to channel ..
                    channel.basicConsume(params[0], true, consumer);


                      try {

                          // close connection after 10 sec listening to port
                          Thread.sleep(10000);
                          channel.close();
                          connection.close();
                          stopSelf();


                      } catch (InterruptedException e) {
                         Log.e("MYAPP", "exception", e);

                      }



            }
            catch ( Exception e) {

                  Log.e("MYAPP", "exception", e);

            }

            return null;
        }

    }

0 个答案:

没有答案