//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));
}
}

Java Messages Synchronous and Asynchronous的更多相关文章

  1. Synchronous and Asynchronous I/O [Windows]

    There are two types of input/output (I/O) synchronization: synchronous I/O and asynchronous I/O. Asy ...

  2. 操作系统OS - 阻塞(Blocking)非阻塞(Non-Blocking)与同步(Synchronous)异步(Asynchronous)

    参考: http://blog.jobbole.com/103290/ https://www.zhihu.com/question/19732473/answer/23434554 http://b ...

  3. Should I expose asynchronous wrappers for synchronous methods?

    Lately I've received several questions along the lines of the following, which I typically summarize ...

  4. Asynchronous Disk I/O Appears as Synchronous on Windows

    Summary File I/O on Microsoft Windows can be synchronous or asynchronous. The default behavior for I ...

  5. Java资源大全中文版(Awesome最新版)

    Awesome系列的Java资源整理.awesome-java 就是akullpp发起维护的Java资源列表,内容包括:构建工具.数据库.框架.模板.安全.代码分析.日志.第三方库.书籍.Java 站 ...

  6. Java开源框架推荐(全)

    Build Tool Tools which handle the buildcycle of an application. Apache Maven - Declarative build and ...

  7. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  8. 【Java】-NO.20.Exam.1.Java.1.001- 【1z0-807】- OCEA

    1.0.0 Summary Tittle:[Java]-NO.20.Exam.1.Java.1.001-[1z0-807] Style:EBook Series:Java Since:2017-10- ...

  9. java设计模式大全 Design pattern samples in Java(最经典最全的资料)

    java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...

随机推荐

  1. loj 1017(dp)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=25843 思路:我们可以发现题目与点的X坐标没有关系,于是可以直接对 ...

  2. XmlPull

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); // 创建解析器. XmlPullParser parser = ...

  3. nignx重启启动关闭

    http://www.cnblogs.com/jianxie/p/3990377.html 一.启动 cd usr/local/nginx/sbin ./nginx cd usr/local/ngin ...

  4. Windows 7下搭建Jmeter测试环境

    jmeter配置.安装 一. 工具描述 apache jmeter是100%的java桌面应用程序,它被设计用来加载被测试软件功能特性.度量被测试软件的性能.设计jmeter的初衷是测试web应用,后 ...

  5. Laravel环境配置之安装Homestead

    laravel requirements: PHP >= 5.5.9   (机器上yum安装的是5.3.3) OpenSSL PHP Extension PDO PHP Extension Mb ...

  6. 使用iterator出现的死循环

    public static void main(String[] args) { List<String> list = new ArrayList<String>(); li ...

  7. DOM--1 遵循最佳实践

    为重用命名空间而进行规划 (function() { function $(id) { return document.getElementById(id); } function alertNode ...

  8. Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  9. Codeforces Round #328 (Div. 2)

    这场CF,准备充足,回寝室洗了澡,睡了一觉,可结果...   水 A - PawnChess 第一次忘记判断相等时A先走算A赢,hack掉.后来才知道自己的代码写错了(摔 for (int i=1; ...

  10. HDU4135 Co-prime(容斥原理)

    题目求[A,B]区间内与N互质数的个数. 可以通过求出区间内与N互质数的个数的前缀和,即[1,X],来得出[A,B]. 那么现在问题是求出[1,X]区间内与N互质数的个数,考虑这个问题的逆问题:[1, ...