ActiveMQ的简单案例

jopen 10年前

这里导入lib包后即可运行代码,具体的流程可以看资料:首先启动activeMQ,再运行send和receive程序!

    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 {            /**            * @param args            */                        public PTP_sends(){                            }                    private Connection connection;            private String subject = "TOOL.DEFAULT";                            public void send(String msg){                //创建connectionFaction                        ActiveMQConnectionFactory connectionFactory =new ActiveMQConnectionFactory("tcp://localhost:61616");                try {                    //创建connection                    connection=connectionFactory.createConnection();                    connection.start();                    //创建session,设置消息确认机制                    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);                    //创建destination                    Destination destination=session.createQueue(subject);                    //创建producer                    MessageProducer producer=session.createProducer(destination);                    //设置JMS的持久性                    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);                    //JMS消息体                    TextMessage message = session.createTextMessage(msg);                                        //发生消息message                    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");                                //代码启动activeMQ        //      BrokerService broker = new BrokerService();        //      try {        //          broker.addConnector("tcp://localhost:61616");        //          broker.start();        //      } catch (Exception e) {        //          e.printStackTrace();        //      }                                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 {            /**        * @param args        */                private Connection connection;        private String subject = "TOOL.DEFAULT";                                 public void receive(){                ActiveMQConnectionFactory connectionFactory =new ActiveMQConnectionFactory();                try {                    //创建connection                    connection=connectionFactory.createConnection();                    connection.start();                    //创建session                    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);                    //创建destination                    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();        }        }  
</div> </div> 来自:http://blog.csdn.net/caiwenfeng_for_23/article/details/8606538