activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From prgtrdr <prgt...@gmail.com>
Subject Re: How can I retrieve a Stomp username when a new Connection is made?
Date Fri, 30 Nov 2012 11:10:13 GMT
Christian,

Thanks for pointing me in the right direction.  Here's my code that seems to
work so others can use it.  That said, I'm really nervous about using
reflection to get at the brokerConnectionStates collection.  There must be a
better way to do this...

       @SuppressWarnings("unchecked")
	public void onMessage(Message message){
		
        if (message instanceof ActiveMQMessage) {
            ActiveMQMessage activeMessage = (ActiveMQMessage) message;
            Object command = activeMessage.getDataStructure();
            if (command instanceof ConsumerInfo) {
                System.out.println("A consumer subscribed to a topic or
queue: " + command);
            } else if (command instanceof RemoveInfo) {
                RemoveInfo removeInfo = (RemoveInfo) command;
                if (removeInfo.isConsumerRemove()) {
                    System.out.println("A consumer unsubscribed from a topic
or queue");
                }
                else {
                    System.out.println("RemoveInfo, a connection was closed:
" + command);
                }
            } else if (command instanceof ConnectionInfo) {
            	//Retrieve this Connection so we can get the username and
password
                TransportConnector connector =
NeutroGate.broker.getConnectorByName("websocket"); 
                List<TransportConnection> connections = 
connector.getConnections(); 
                for (TransportConnection c : connections) {
                	// use reflection to get at field 
                	Field f = null;
					try {
						f =
TransportConnection.class.getDeclaredField("brokerConnectionStates");
					} catch (NoSuchFieldException | SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
                	f.setAccessible(true); 
                	Map<ConnectionId, ConnectionState> context = null;
					try {
						context = (Map<ConnectionId, ConnectionState>) f.get(c);
					} catch (IllegalArgumentException | IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                	ConnectionId commandCid = ((ConnectionInfo)
command).getConnectionId();
                	for (ConnectionId id : context.keySet()) { 
                    	if (id.equals(commandCid)) {
	                		String username = 
context.get(id).getInfo().getUserName(); 
	                		String password = 
context.get(id).getInfo().getPassword(); 
	                        System.out.println("ConnectionInfo, a new
connection was made to " + username + ":" + password + ": " + command);

                    	}
                	}
 	            }
            } else {
            	System.out.println("Unknown command: " + command);
            }
        }




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

Mime
View raw message