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));
}
}
Java Messages Synchronous and Asynchronous的更多相关文章
- Synchronous and Asynchronous I/O [Windows]
There are two types of input/output (I/O) synchronization: synchronous I/O and asynchronous I/O. Asy ...
- 操作系统OS - 阻塞(Blocking)非阻塞(Non-Blocking)与同步(Synchronous)异步(Asynchronous)
参考: http://blog.jobbole.com/103290/ https://www.zhihu.com/question/19732473/answer/23434554 http://b ...
- Should I expose asynchronous wrappers for synchronous methods?
Lately I've received several questions along the lines of the following, which I typically summarize ...
- 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 ...
- Java资源大全中文版(Awesome最新版)
Awesome系列的Java资源整理.awesome-java 就是akullpp发起维护的Java资源列表,内容包括:构建工具.数据库.框架.模板.安全.代码分析.日志.第三方库.书籍.Java 站 ...
- Java开源框架推荐(全)
Build Tool Tools which handle the buildcycle of an application. Apache Maven - Declarative build and ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- 【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- ...
- java设计模式大全 Design pattern samples in Java(最经典最全的资料)
java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...
随机推荐
- C#学习笔记(三)——流程控制
一.布尔逻辑 1.与布尔有关的2元运算符 2.布尔运算符 PS:“&”与“&&“之类的区别 (1)”&“是按位运算,也就是说是将2个数都转换成2进制,然后逐个进行与操作 ...
- selector选择器
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="ht ...
- WebStorm9
下载地址: http://www.onlinedown.net/soft/554406.htm 注册码: UserName:William===== LICENSE BEGIN =====45550- ...
- SU suacor命令学习
前段时间事情忙,今天才更新.
- 资产移动盘点手持机PDA系统
手持机PDA系统功能 PDA初始化 从后台管理机系统中预先设置的众多操作人员列表中下载当前PDA的使用人员: 系统支持多用户使用同一台PDA情况下的用户认证登陆,每一用户根据后台管理机系统设置与安全管 ...
- POJ1191 棋盘分割(DP)
化简一下那个方差得到:$$\sqrt\frac{(\Sigma_{i=1}^nx_i)-n\bar x^2}{n}$$ 除了$\Sigma_{i=1}^nx_i$这部分未知,其余已知,而那部分显然越大 ...
- 20145302张薇《Java程序设计》实验三报告
20145302张薇<Java程序设计>实验三:敏捷开发与XP实践 实验内容 使用git上传代码 使用git实现代码开发实践 实现代码的重载 使用git上传代码 git init git ...
- 李洪强-C语言5-函数
C语言函数 一.函数 C语言程序是由函数构成的,每个函数负责完成一部分的功能,函数将工恩呢该封装起来,以供程序调用. 二.函数定义 目的:将一些常用的功能封装起来,以供日后调用. 步骤:确定函数名,确 ...
- [转] - 使用Qt作窗口截屏(含源码)
截屏(screenshot),就是将屏幕上的东西拷贝下来存成图片文件.介绍的好像有点多余:(,那我们就直接切入正题. QPixmap提供了两个函数grabWidget和grabWindow可以将屏幕上 ...
- php 的curl 模拟登陆
做一个类似这样的web 应用. 1,解决掉验证码 其实这是正方的一个小bug,当我们进入登陆界面时,浏览器会去请求服务器,服务器会生成一个验证码图片.如果我们不去请求这个图片,那么正方后台也不会生成相 ...