资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Java调用ActiveMQ简单范例的代码

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

创新互联技术团队10多年来致力于为客户提供成都网站设计、成都网站建设品牌网站设计营销型网站建设、搜索引擎SEO优化等服务。经过多年发展,公司拥有经验丰富的技术团队,先后服务、推广了近1000家网站,包括各类中小企业、企事单位、高校等机构单位。

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

}  

分享标题:Java调用ActiveMQ简单范例的代码
转载源于:http://cdkjz.cn/article/pjiscs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220