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

最新文章

  1. 代替jquery $.post 跨域提交数据的N种形式
  2. Java高并发同步Volatile的使用
  3. JQuery easyui 笔记
  4. 解决winrar压缩软件弹出广告
  5. svn out of date
  6. paypal api 相关资料
  7. 【Linux常识篇(1)】所谓的正向代理与反向代理
  8. Java网络编程(TCP客户端)
  9. [转]ODBC连接ORACLE数据库的设置
  10. 对中级Linux 用户非常有用的20 个命令
  11. socket的accept函数解析
  12. 常见的数据扩充(data augmentation)方法
  13. MariaDB:开启日志记录SQL
  14. 如何使用命令从linux服务器下载文件到windows
  15. [leetcode]243. Shortest Word Distance最短单词距离
  16. cf666E. Forensic Examination(广义后缀自动机 线段树合并)
  17. 【转帖】M1、M2增速
  18. Openvas安装
  19. html5-select和datalist元素
  20. 添加快捷键 ShortCut

热门文章

  1. hdu 4751(dfs染色)
  2. FPGA学习
  3. 【转】有赞的kylin方案
  4. spring获取webapplicationcontext,applicationcontext几种方法详解
  5. 模拟 ZOJ 3878 Convert QWERTY to Dvorak
  6. JavaScript判断用户是通过电脑端还是移动端访问
  7. BZOJ3488 : [ONTAK2010]Highways
  8. Autoencoder
  9. 利用onekeyup即可实现验证码的点击刷新功能
  10. 【液晶模块系列基础视频】4.3.X-GUI图形界面库-画box函数简介