How to get Messages by the consumer according to priority of the messages set by the publishers RabbitMQ -
i have publish messages priority set single consumer(i.e single consumer may receive messages according message priority). want messages , print them according message priority on consumer side. hey guys me out in !
public class send extends thread { int priority; string name = ""; string app_type = ""; private static final string exchange_name = "topic_exchange"; public void run() { connectionfactory connfac = new connectionfactory(); connfac.sethost("localhost"); try { connection conn = connfac.newconnection(); channel channel = conn.createchannel(); channel.exchangedeclare(exchange_name, builtinexchangetype.topic); for(int j=1; j<=200; j++) { randomwait(); int random = (int)(math.random() * 10 + 1); string routingkey = j+"."+"update"+"."+app_type; string msg = name; channel.basicpublish(exchange_name, routingkey, new amqp.basicproperties.builder() .contenttype("text/plain") .deliverymode(2) .priority(priority) .build(), msg.getbytes("utf-8")); system.out.println("sent " + routingkey + " : " + msg + " "+" priority : "+priority); } channel.close(); conn.close(); } catch (ioexception ex) { logger.getlogger(send.class.getname()).log(level.severe, null, ex); system.out.println("exception1 :--"+ex); } catch (timeoutexception ex) { logger.getlogger(send.class.getname()).log(level.severe, null, ex); system.out.println("exception 2:--"+ex); } } void randomwait() { try { thread.currentthread().sleep((long)(200*math.random())); } catch (interruptedexception x) { system.out.println("interrupted!"); } } public static void main(string[] args) { // todo code application logic here send test1 = new send(); test1.name = "hello android"; test1.app_type = "android"; test1.priority = 10; send test2 = new send(); test2.name = "hello android"; test2.app_type = "android"; test2.priority = 5; test1.start(); test2.start(); } } in above code have use thread pass priority , message value , started both thread @ same time publish messages different priorities. have set priority value in amq builder.
the queue has configured support priority.
Comments
Post a Comment