ACTIVEMQ Mqtt Point-to-point chat implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.