activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Posta <christian.po...@gmail.com>
Subject Re: How can I retrieve a Stomp username when a new Connection is made?
Date Thu, 29 Nov 2012 21:51:54 GMT
Yah. You might be using nabble? I guess if you're sending it via email the
attachments don't show up.

I'll paste here:

package org.apache.activemq;

import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnection;
import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.command.ConnectionId;
import org.apache.activemq.state.ConnectionState;
import org.apache.activemq.transport.stomp.Stomp;
import org.apache.activemq.transport.stomp.StompConnection;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author <a href="http://www.christianposta.com/blog">Christian Posta</a>
 */
public class GettingUserNameFromTransportConnectionTest {

    @Test
    public void testGetUserName() throws Exception {
        BrokerService service = new BrokerService();
        service.setUseJmx(true);
        service.addConnector("stomp://localhost:61613");
        service.setPopulateUserNameInMBeans(true);
        service.start();
        service.waitUntilStarted();

        // create a connection
        StompConnection connection = new StompConnection();
        connection.open("localhost", 61613);
        String connectFrame = "CONNECT\n" + "login:system\n" +
"passcode:manager\n" + "request-id:1\n" + "\n" + Stomp.NULL;
        connection.sendFrame(connectFrame);

        String response = connection.receiveFrame();
        System.out.println("STOMP response: " + response);
        TimeUnit.SECONDS.sleep(1);

        TransportConnector connector =
service.getConnectorByName("stomp://localhost:61613");
        List<TransportConnection> connections =  connector.getConnections();
        for (TransportConnection c : connections) {
//            if (c.getConnectionId().equals("some conn ID")) {
                // use reflection to get at field
            Field f =
TransportConnection.class.getDeclaredField("brokerConnectionStates");
            f.setAccessible(true);
            Map<ConnectionId, ConnectionState> context = (Map<ConnectionId,
ConnectionState>) f.get(c);
            for (ConnectionId id : context.keySet()) {
                String username =  context.get(id).getInfo().getUserName();
                System.out.println("User name: " + username);
            }
//            }
        }

    }
}



On Thu, Nov 29, 2012 at 2:07 PM, prgtrdr <prgtrdr@gmail.com> wrote:

> Attachment??
>
>
>
> --
> View this message in context:
> http://activemq.2283324.n4.nabble.com/How-can-I-retrieve-a-Stomp-username-when-a-new-Connection-is-made-tp4659949p4659964.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.
>



-- 
*Christian Posta*
http://www.christianposta.com/blog
twitter: @christianposta

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message