I think of a point-to-point chat method, do not have to cut people have to build a topic, the idea is to customize a distribution strategy, specifically as follows:
1, to establish a topic, everyone with a matching subscription to subscribe to the topic as the head of the topic, for example: everyone subscribes to ptp/#.
2, for example, a to B to send chat information, b ClientID is bbb,a only need to push the chat information to ptp/bbb, I wrote a custom policy for all PTP beginning topic do custom distribution <policyentry topic= "ptp.>" , the ClientID in topic is parsed out and the message is sent only to B.
Custom Policy code:
Public classClientidfilterdispatchpolicyextendsSimpledispatchpolicy { Public BooleanDispatch (messagereference node, Messageevaluationcontext msgcontext, List<Subscription>consumers)throwsException {System.out.println ("--------------------------------------------------------------------------."); System.out.println (Node.getmessage (). Getdestination (). Getqualifiedname ()); System.out.println (Node.getmessage (). Getdestination (). Getphysicalname ()); String Topic=node.getmessage (). Getdestination (). Getphysicalname (); String clientId= Topic.substring (Topic.indexof (".") +1, Topic.length ()); System.out.println ("ClientId:" +clientId); System.out.println ("--------------------------------End------------------------------------------"); if(ClientId = =NULL) Super. Dispatch (node, msgcontext, consumers); Activemqdestination Destination=node.getmessage (). Getdestination (); intCount = 0; for(Subscription sub:consumers) {if(Sub.getconsumerinfo (). IsBrowser ()) {Continue; } if(!sub.matches (node, msgcontext)) {sub.unmatched (node); Continue; } System.out.println ("Istopic:" +destination.istopic ()); System.out.println ("Getclientid:" +Sub.getcontext (). Getclientid ()); if((ClientId! =NULL) &&(Destination.istopic ())&&(Clientid.equals (Sub.getcontext (). Getclientid ()))) {Sub.add (node); Count++; } Else{sub.unmatched (node); } } returnCount > 0; } }
Activemq.xml
<policyentry topic= "ptp.>" > <dispatchPolicy> <clientIdFilterDispatchPolicy/> </dispatchPolicy> </policyEntry>
ACTIVEMQ Mqtt Point-to-point chat implementation