Java Messages Synchronous and Asynchronous
2024-08-26 17:20:51
//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));
}
}
最新文章
- 代替jquery $.post 跨域提交数据的N种形式
- Java高并发同步Volatile的使用
- JQuery easyui 笔记
- 解决winrar压缩软件弹出广告
- svn out of date
- paypal api 相关资料
- 【Linux常识篇(1)】所谓的正向代理与反向代理
- Java网络编程(TCP客户端)
- [转]ODBC连接ORACLE数据库的设置
- 对中级Linux 用户非常有用的20 个命令
- socket的accept函数解析
- 常见的数据扩充(data augmentation)方法
- MariaDB:开启日志记录SQL
- 如何使用命令从linux服务器下载文件到windows
- [leetcode]243. Shortest Word Distance最短单词距离
- cf666E. Forensic Examination(广义后缀自动机 线段树合并)
- 【转帖】M1、M2增速
- Openvas安装
- html5-select和datalist元素
- 添加快捷键 ShortCut
热门文章
- hdu 4751(dfs染色)
- FPGA学习
- 【转】有赞的kylin方案
- spring获取webapplicationcontext,applicationcontext几种方法详解
- 模拟 ZOJ 3878 Convert QWERTY to Dvorak
- JavaScript判断用户是通过电脑端还是移动端访问
- BZOJ3488 : [ONTAK2010]Highways
- Autoencoder
- 利用onekeyup即可实现验证码的点击刷新功能
- 【液晶模块系列基础视频】4.3.X-GUI图形界面库-画box函数简介