• Java Messages Synchronous and Asynchronous


    //The Consumer Class Consumes Messages in a Synchronous Manner
    
    public class Consumer {
    public static void main(String[] args) {
    try {
    // Gets the JNDI context
    Context jndiContext = new InitialContext();
    // Looks up the administered objects
    ConnectionFactory connectionFactory = (ConnectionFactory) 
    jndiContext.lookup("jms/javaee7/ConnectionFactory");
    Destination queue = (Destination) jndiContext.lookup("jms/javaee7/Queue");
    // Loops to receive the messages
    try (JMSContext context = connectionFactory.createContext()) {
    while (true) {
    String message = context.createConsumer(queue).receiveBody(String.class);
    }
    }
    } catch (NamingException e) {
    e.printStackTrace();
    }
    }
    }
    //The Consumer Is a Message Listener
    
    public class Listener implements MessageListener {
    public static void main(String[] args) {
    try {
    // Gets the JNDI context
    Context jndiContext = new InitialContext();
    // Looks up the administered objects
    ConnectionFactory connectionFactory = (ConnectionFactory) 
    jndiContext.lookup("jms/javaee7/ConnectionFactory");
    Destination queue = (Destination) jndiContext.lookup("jms/javaee7/Queue");
    try (JMSContext context = connectionFactory.createContext()) {
    context.createConsumer(queue).setMessageListener(new Listener());
    }
    } catch (NamingException e) {
    e.printStackTrace();
    }
    }
    public void onMessage(Message message) {
    System.out.println("Async Message received: " + message.getBody(String.class));
    }
    }
  • 相关阅读:
    idapython常用api记录7.0
    Ubuntu(16.0.4)上编译android8.1源码(资料最全版本)
    Frida常用方法
    Frida Java Hook 详解(安卓9):代码及示例(下)
    Frida Java Hook 详解(安卓9):代码及示例(上)
    windows命令行工具导出系统日志——wevtutil
    帆软 V9 Getshell
    和信创天云桌面命令执行
    天擎越权访问
    天擎-前台SQL注入
  • 原文地址:https://www.cnblogs.com/rojas/p/5310745.html
Copyright © 2020-2023  润新知