日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

 

創建ActiveMQ連接:

import JAVAx.jms.*;
import org.Apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void mAIn(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 啟動連接
            connection.start();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

創建ActiveMQ會話:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

創建ActiveMQ隊列:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建隊列
            Queue queue = session.createQueue("myQueue");
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

創建ActiveMQ主題:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建主題
            Topic topic = session.createTopic("myTopic");
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

發送消息到隊列:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建隊列
            Queue queue = session.createQueue("myQueue");
            
            // 創建消息生產者
            MessageProducer producer = session.createProducer(queue);
            
            // 創建消息
            TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
            
            // 發送消息
            producer.send(message);
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

發送消息到主題:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建主題
            Topic topic = session.createTopic("myTopic");
            
            // 創建消息生產者
            MessageProducer producer = session.createProducer(topic);
            
            // 創建消息
            TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
            
            // 發送消息
            producer.send(message);
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

接收隊列中的消息:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建隊列
            Queue queue = session.createQueue("myQueue");
            
            // 創建消息消費者
            MessageConsumer consumer = session.createConsumer(queue);
            
            // 接收消息
            Message message = consumer.receive();
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                System.out.println("Received message: " + textMessage.getText());
            }
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

接收主題中的消息:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建主題
            Topic topic = session.createTopic("myTopic");
            
            // 創建消息消費者
            MessageConsumer consumer = session.createConsumer(topic);
            
            // 接收消息
            Message message = consumer.receive();
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                System.out.println("Received message: " + textMessage.getText());
            }
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

發布-訂閱模式中的發布者:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建主題
            Topic topic = session.createTopic("myTopic");
            
            // 創建消息生產者
            MessageProducer producer = session.createProducer(topic);
            
            // 創建消息
            TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
            
            // 發送消息
            producer.send(message);
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

發布-訂閱模式中的訂閱者:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建主題
            Topic topic = session.createTopic("myTopic");
            
            // 創建消息消費者
            MessageConsumer consumer = session.createConsumer(topic);
            
            // 接收消息
            Message message = consumer.receive();
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                System.out.println("Received message: " + textMessage.getText());
            }
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

使用消息監聽器接收隊列中的消息:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建隊列
            Queue queue = session.createQueue("myQueue");
            
            // 創建消息消費者
            MessageConsumer consumer = session.createConsumer(queue);
            
            // 注冊消息監聽器
            consumer.setMessageListener(new MessageListener() {
                public void onMessage(Message message) {
                    if (message instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) message;
                        try {
                            System.out.println("Received message: " + textMessage.getText());
                        } catch (JMSException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            
            // 啟動連接
            connection.start();
            
            // 等待接收消息
            Thread.sleep(5000);
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

使用消息監聽器接收主題中的消息:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建主題
            Topic topic = session.createTopic("myTopic");
            
            // 創建消息消費者
            MessageConsumer consumer = session.createConsumer(topic);
            
            // 注冊消息監聽器
            consumer.setMessageListener(new MessageListener() {
                public void onMessage(Message message) {
                    if (message instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) message;
                        try {
                            System.out.println("Received message: " + textMessage.getText());
                        } catch (JMSException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            
            // 啟動連接
            connection.start();
            
            // 等待接收消息
            Thread.sleep(5000);
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

使用消息選擇器接收隊列中的消息:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建隊列
            Queue queue = session.createQueue("myQueue");
            
            // 創建消息消費者
            MessageConsumer consumer = session.createConsumer(queue, "age > 18");
            
            // 接收消息
            Message message = consumer.receive();
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                System.out.println("Received message: " + textMessage.getText());
            }
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

使用消息選擇器接收主題中的消息:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
            // 創建主題
            Topic topic = session.createTopic("myTopic");
            
            // 創建消息消費者
            MessageConsumer consumer = session.createConsumer(topic, "age > 18");
            
            // 接收消息
            Message message = consumer.receive();
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                System.out.println("Received message: " + textMessage.getText());
            }
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

使用事務發送消息:

import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;

public class ActiveMQExample {
    public static void main(String[] args) {
        try {
            // 創建連接工廠
            ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            
            // 創建連接
            Connection connection = factory.createConnection();
            
            // 創建會話
            Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
            
            // 創建隊列
            Queue queue = session.createQueue("myQueue");
            
            // 創建消息生產者
            MessageProducer producer = session.createProducer(queue);
            
            // 創建消息
            TextMessage message = session.createTextMessage("Hello, ActiveMQ!");
            
            // 發送消息
            producer.send(message);
            
            // 提交事務
            session.commit();
            
            // 關閉會話
            session.close();
            
            // 關閉連接
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

這些示例覆蓋了ActiveMQ的常用用法,包括創建連接、創建會話、創建隊列和主題、發送和接收消息等。你可以根據自己的需求選擇相應的示例進行參考和使用。

分享到:
標簽:ActiveMQ
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定