一起来学activeMQ 5.pub/sub ,non-persistent/persistent,queue

不再废话,一切由代码说话

1.queue

producer:

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.151.128:61616");
        Connection connection = null;
        Session session = null;
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            Destination destination = session.createQueue("my-queue");

            MessageProducer producer = session.createProducer(destination);
            producer.setTimeToLive(60);

            for(int i=0;i<3;i++){
                MapMessage messgae = session.createMapMessage();
                messgae.setString("message---"+i, "msg"+i);
                messgae.setStringProperty("hellow", "world");

                producer.send(messgae);
                System.out.println(i);
            }
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                if (session != null) {
                    session.close();
                }
            } catch (JMSException e2) {
            }
        }

consumer:

ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.151.128:61616");
        Connection connection = null;
        Session session = null;
        try {
            connection = connectionFactory.createConnection();
            connection.start();

            Enumeration<String> enumerations = connection.getMetaData().getJMSXPropertyNames();
            while (enumerations.hasMoreElements()) {
                System.out.println("name:" + enumerations.nextElement());
            }

            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            Destination destination = session.createQueue("my-queue");
            MessageConsumer consumer = session.createConsumer(destination);

            for (int i = 0; i < 30; i++) {
                MapMessage msg = (MapMessage) consumer.receive();// 接受一次消息

                System.out.println("prototype:" + msg.getStringProperty("hellow"));
                System.out.println("msg:" + msg.getString("message---" + i));
            }
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                if (session != null) {
                    session.close();
                }
            } catch (JMSException e2) {
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 `subprocess.Popen` 来执行带有很多入参的 Python 脚本,并得到它的 PID。下面是一个例子: ```python import subprocess # 定义命令行参数 args = ["python", "script.py", "/opt/hikvision/web/components/tomcat85linux64.1/", "/opt/hikvision/web/components/xconfig.1/bin/xconfig-server/config", "/opt/hikvision/web/components/sac.1/bin/web", "/opt/hikvision/web/components/activemq514linux64.1/bin/linux-x86-64", "/opt/hikvision/web/components/rabbitmq.1/bin/rabbitmq-auth/config", "/opt/hikvision/web/components/ls.1/script/license", "/opt/hikvision/web/components/xresmgr.1/bin/xresmgr-rms/config", "/opt/hikvision/web/components/loganalysis.1/bin/loges", "/opt/hikvision/web/components/xjob.1/bin/xjob-server/config", "/opt/hikvision/web/components/xdictionary.1/bin/xdictionary-dds/config"] # 执行脚本 process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # 获取进程 ID pid = process.pid # 打印进程 ID print("PID:", pid) # 获取输出 output, error = process.communicate() # 打印输出和错误 print(output.decode()) print(error.decode()) ``` 上述代码中,`args` 变量定义了要执行的命令和参数。在这个例子中,我们要执行 `script.py` 脚本,并传入多个命令行参数,每个参数都是一个路径。 然后,我们使用 `subprocess.Popen` 来执行这个命令。`stdout=subprocess.PIPE` 和 `stderr=subprocess.PIPE` 参数表示要把标准输出和标准错误输出都保存下来,以便后面使用。 接着,我们使用 `process.pid` 属性来获取进程的 PID,并将其保存在变量 `pid` 中。 最后,我们打印进程的 PID,获取脚本的输出和错误信息,并通过 `decode()` 方法将字节字符串转换为文本字符串。最后,我们打印输出和错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值