温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java调用ActiveMQ简单范例的代码

发布时间:2020-07-14 11:36:01 来源:网络 阅读:333 作者:Dmimi 栏目:编程语言

如下的内容是关于Java调用ActiveMQ简单范例的内容,应该能对大伙有所用。

package jms;  

import javax.jms.Connection;  
import javax.jms.DeliveryMode;  
import javax.jms.Destination;  
import javax.jms.JMSException;  
import javax.jms.MessageProducer;  
import javax.jms.Session;  
import javax.jms.TextMessage;  

import org.apache.activemq.ActiveMQConnectionFactory;  
import org.apache.activemq.broker.BrokerService;  
import org.apache.log4j.PropertyConfigurator;  

public class PTP_sends {  

    public PTP_sends(){  

    }  

    private Connection connection;  
    private String subject = "TOOL.DEFAULT";  

    public void send(String msg){  

        try {  
            connection=connectionFactory.createConnection();  
            connection.start();  
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
            Destination destination=session.createQueue(subject);  
            MessageProducer producer=session.createProducer(destination);  
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);  
            TextMessage message = session.createTextMessage(msg);  

            producer.send(message);  
            System.out.println("消息已经发送。。。。");  

            message.clearProperties();  
            session.close();  
            connection.stop();  
            connection.close();  
            System.out.println("关闭资源。。。。");  
        } catch (JMSException e) {  
            e.printStackTrace();  
        }  
    }  

    public static void main(String[] args) {  
        PropertyConfigurator.configure("c:\log4j.properties");  

        PTP_sends ptpSends=new PTP_sends();  
        ptpSends.send("this is JMS .....");  
    }  

}  

package jms;  

import javax.jms.Connection;  
import javax.jms.Destination;  
import javax.jms.JMSException;  
import javax.jms.Message;  
import javax.jms.MessageConsumer;  
import javax.jms.Session;  
import javax.jms.TextMessage;  

import org.apache.activemq.ActiveMQConnectionFactory;  
import org.apache.log4j.PropertyConfigurator;  

public class PTP_receive {  

    private Connection connection;  
    private String subject = "TOOL.DEFAULT";  

     public void receive(){  
            ActiveMQConnectionFactory connectionFactory =new ActiveMQConnectionFactory();  
            try {  
                connection=connectionFactory.createConnection();  
                connection.start();  
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
                Destination destination=session.createQueue(subject);  
                MessageConsumer consumer=session.createConsumer(destination);  
                System.out.println("同步接受消息:");  
                Message message=consumer.receive();  
                System.out.println("n收到的message 是:"+((TextMessage)message).getText());  

                message.clearProperties();  
                consumer.close();  
                session.close();  
                connection.stop();  
                connection.close();  
                System.out.println("关闭资源。。。。");  
            } catch (JMSException e) {  
                e.printStackTrace();  
            }  
        }  

    public static void main(String[] args) {  
        PropertyConfigurator.configure("c:\log4j.properties");  
        PTP_receive receive=new PTP_receive();  
        receive.receive();  
    }  

}  
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI