コード例 #1
0
  @Override
  public String amqpRegister(User user) throws Exception {

    try {
      Channel rabbitMqChannel = rabbitMqConnection.createChannel();

      // create or get user queue
      AMQP.Queue.DeclareOk queue =
          rabbitMqChannel.queueDeclare(
              "USER_".concat(String.valueOf(user.hashCode())),
              true, // durable
              false, // exclusive
              false, // auto delete
              null // no arguments
              );

      Map<String, Object> bindParams = new HashMap<>();
      bindParams.put(ARGUMENT_TO_ID, user.getId());
      rabbitMqChannel.queueBind(queue.getQueue(), amqpExchange, "", bindParams);
      channelListener.registerChannel(rabbitMqChannel, queue.getQueue(), false);

      // add user channel to active channels
      USER_CHANNEL.put(user, rabbitMqChannel);
      return queue.getQueue();

    } catch (Exception e) {
      logger.info("### Couldn't create user queue .. -> {" + e.getMessage() + "}");
      throw e;
    }
  }
コード例 #2
0
  @Override
  public void amqpUnRegister(User user) throws Exception {
    Channel channel = USER_CHANNEL.get(user);
    if (channel != null) {
      logger.info("###Unregistering user [" + user.getUsername() + "] ... ");
      // delete queue
      channel.queueDelete("USER_".concat(String.valueOf(user.hashCode())));
      USER_CHANNEL.remove(user);

      // close channel
      channel.close();
    }
    logger.info("###Unregistering user [" + user.getUsername() + "] Success !");
  }