You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by aougav <ao...@hotmail.com> on 2008/04/03 10:04:27 UTC

solution how to correlate between client users and connections

Dear All,

To correlate between client users and actual connections we need to pass
info to the connection.
General Setup:
 1. Server is listening on topic.subject1 advisory topic to get
connection/disconnect information.
 2. Client user2 connects to broker [server] to consume topic.subject1
messages.
   2.1 Server listener gets some info. 
   2.2 We want the server to link between user2 and this particular
connection.
 3. Client stop connection
   3.1 Server listener gets some info. 
   3.2 We want the server to be sure user2 (the same (2.1)) stop consuming
messages from topic.subject1. He/she disconnect from the application.

Key point to the solution:
  We will change the connectionid, which is also the consumerid, by adding a
username/userid as the mean to link between client user and connection.

Solution:
1. On the server side
          org.apache.activemq.command.ActiveMQDestination dest = 
            ActiveMQDestination.createDestination(
              "topic.subject1",
              ActiveMQDestination.TOPIC_TYPE);
          adminTopic = AdvisorySupport.getConsumerAdvisoryTopic(dest);
          //log.info("listen to admin topic: "+adminTopic);
          consumer = session.createConsumer(adminTopic);
          consumer.setMessageListener(this);

2. The client connect and MUST add his username/useridentification (some id
mean) to the connection. Example using "user2" constant:

           ActiveMQConnection topic_qconn = (ActiveMQConnection) 
                  mqcf.createQueueConnection();
           // change connectionid/consumerid before starting the connection
           topic_qconn.getConnectionInfo().getConnectionId().
                  setValue("-user2-"+idgenerator.generateId()); start
           topic_qconn.start();

Note that we must have a unique connection id for each connection, so we use
        org.apache.activemq.util.IdGenerator idgenerator = null; // added to
object
 and
        if (idgenerator == null) // only once - the client init the
idgenerator
            idgenerator = IdGenerator();

3. The server listener is getting the advisory message
    in the
       public void onMessage(javax.jms.Message msg)
    we use (omitting try/catch etc)
       if (msg instanceof ActiveMQMessage) {
          ActiveMQMessage aMsg =  (ActiveMQMessage)msg;
          Object obj = aMsg.getDataStructure();
          if (obj instanceof ConsumerInfo) {
                // consumer to topic/queue is starting a session to it <==
connect
                ConsumerInfo info = (ConsumerInfo) obj;
                ConsumerId cid = info.getConsumerId();
                // ... here we get cid = "-user2-ID:...uniquegeneratedid..."
                .... keep/save info...
          } else if (obj instanceof RemoveInfo) {
                // Removes a consumer, producer, session or connection.
                RemoveInfo info = (RemoveInfo) obj;
                if (info.getObjectId() instanceof ConsumerId) {
                  // consumer of topic/queue is disconnect from topic/queue 
<== disconnect
                  ConsumerId cid = (ConsumerId) info.getObjectId();
                  // ... here we get cid =
"-user2-ID:...uniquegeneratedid..."
                  .... process user2 disconnect from "application"...
                }
           }

Best regards
-- 
View this message in context: http://www.nabble.com/solution-how-to-correlate-between-client-users-and-connections-tp16467306s2354p16467306.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.