• 【Active入门】ActiveMQ学习-1


    基本概念:



    接收者:

    1. package com.ll.activemq;
    2. import java.util.Collection;
    3. import java.util.Iterator;
    4. import javax.jms.Connection;
    5. import javax.jms.ConnectionFactory;
    6. import javax.jms.Destination;
    7. import javax.jms.MapMessage;
    8. import javax.jms.MessageConsumer;
    9. import javax.jms.Session;
    10. import javax.jms.TextMessage;
    11. import org.apache.activemq.ActiveMQConnection;
    12. import org.apache.activemq.ActiveMQConnectionFactory;
    13. public class Receiver {
    14. public static void main(String[] args) {
    15. // ConnectionFactory :连接工厂,JMS 用它创建连接
    16. ConnectionFactory connectionFactory;
    17. // Connection :JMS 客户端到JMS Provider 的连接
    18. Connection connection = null;
    19. // Session: 一个发送或接收消息的线程
    20. Session session;
    21. // Destination :消息的目的地;消息发送给谁.
    22. Destination destination;
    23. // 消费者,消息接收者
    24. MessageConsumer consumer;
    25. connectionFactory = new ActiveMQConnectionFactory(
    26. ActiveMQConnection.DEFAULT_USER,
    27. ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
    28. try {
    29. // 构造从工厂得到连接对象
    30. connection = connectionFactory.createConnection();
    31. // 启动
    32. connection.start();
    33. // 获取操作连接
    34. session = connection.createSession(Boolean.FALSE,
    35. Session.AUTO_ACKNOWLEDGE);
    36. // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
    37. destination = session.createQueue("FirstQueue");
    38. consumer = session.createConsumer(destination);
    39. // consumer2 = session.createConsumer(destination);
    40. while (true) {
    41. // 设置接收者接收消息的时间,为了便于测试,这里谁定为100s
    42. TextMessage message = (TextMessage) consumer.receive(28000);
    43. // Object message = (Object) consumer.receive(28000);
    44. if (null != message) {
    45. System.out.println("收到消息" + message.getText());
    46. } else {
    47. System.out.println("时间到... ");
    48. break;
    49. }
    50. }
    51. } catch (Exception e) {
    52. e.printStackTrace();
    53. } finally {
    54. try {
    55. if (null != connection)
    56. connection.close();
    57. } catch (Throwable ignore) {
    58. }
    59. }
    60. }
    61. }

    发送者:

    1. package com.ll.activemq;
    2. import java.util.Collection;
    3. import java.util.Iterator;
    4. import javax.jms.Connection;
    5. import javax.jms.ConnectionFactory;
    6. import javax.jms.Destination;
    7. import javax.jms.MapMessage;
    8. import javax.jms.MessageConsumer;
    9. import javax.jms.Session;
    10. import javax.jms.TextMessage;
    11. import org.apache.activemq.ActiveMQConnection;
    12. import org.apache.activemq.ActiveMQConnectionFactory;
    13. public class Receiver {
    14. public static void main(String[] args) {
    15. // ConnectionFactory :连接工厂,JMS 用它创建连接
    16. ConnectionFactory connectionFactory;
    17. // Connection :JMS 客户端到JMS Provider 的连接
    18. Connection connection = null;
    19. // Session: 一个发送或接收消息的线程
    20. Session session;
    21. // Destination :消息的目的地;消息发送给谁.
    22. Destination destination;
    23. // 消费者,消息接收者
    24. MessageConsumer consumer;
    25. connectionFactory = new ActiveMQConnectionFactory(
    26. ActiveMQConnection.DEFAULT_USER,
    27. ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616");
    28. try {
    29. // 构造从工厂得到连接对象
    30. connection = connectionFactory.createConnection();
    31. // 启动
    32. connection.start();
    33. // 获取操作连接
    34. session = connection.createSession(Boolean.FALSE,
    35. Session.AUTO_ACKNOWLEDGE);
    36. // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
    37. destination = session.createQueue("FirstQueue");
    38. consumer = session.createConsumer(destination);
    39. // consumer2 = session.createConsumer(destination);
    40. while (true) {
    41. // 设置接收者接收消息的时间,为了便于测试,这里谁定为100s
    42. TextMessage message = (TextMessage) consumer.receive(28000);
    43. // Object message = (Object) consumer.receive(28000);
    44. if (null != message) {
    45. System.out.println("收到消息" + message.getText());
    46. } else {
    47. System.out.println("时间到... ");
    48. break;
    49. }
    50. }
    51. } catch (Exception e) {
    52. e.printStackTrace();
    53. } finally {
    54. try {
    55. if (null != connection)
    56. connection.close();
    57. } catch (Throwable ignore) {
    58. }
    59. }
    60. }
    61. }








  • 相关阅读:
    ora04063:view view_test has errors
    toad for oracle中登入界面Connect using框和TNSNames Editor框的显示状态
    格式化时间,转为中文
    一些简单有用的方法合集
    C#加密解密
    计算员工有效工作时间(C#版)
    计算员工有效工作时间(sql版)
    JavaScript 语言基础知识点总结(思维导图)
    正则表达式
    C#中请不要混淆引用类型和ref引用传参
  • 原文地址:https://www.cnblogs.com/ssslinppp/p/4461277.html
Copyright © 2020-2023  润新知