ActiveMQ簡單入門實例

第一步:下載ActiveMQ  官網地址:http://activemq.apache.org/  ;將下載好的壓縮包解壓出來。E:\study\apache-activemq-5.9.1\bin下面有個activemq.bat執行腳本直接啓動mq(須要安裝JDK而且配置環境變量)。(此處路徑是個人路徑)java

第二步:建立一個普通的java項目。將E:\study\apache-activemq-5.9.1下面的activemq-all-5.9.1.jar包拷貝到項目中。apache

第三步:編寫一個簡單的客戶端聊天程序tcp

Chat.javaide


package org.walden.chp02;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.NamingException;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;


public class Chat implements MessageListener{
private TopicSession pubSession;
private TopicPublisher publisher;
private TopicConnection connection;
private String username;

/**
* 初始化構造函數
* 建立一個新的實例 Chat.
*
* @param username
* @throws NamingException 
* @throws JMSException 
*/
public Chat(String username) throws NamingException, JMSException{
//建立一個JMS鏈接工廠。並建立JMS鏈接
TopicConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616");
TopicConnection connection = factory.createTopicConnection();

//建立兩個JMS會話對象
TopicSession pubSession = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
TopicSession subSession = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);

//建立一個JMS主題
Topic chatTopic = new ActiveMQTopic("topic");//主題名字隨便寫

//建立一個JMS訂閱者與發佈者.createPublisher附帶的參數是一個消息
TopicPublisher publisher = pubSession.createPublisher(chatTopic);
//選擇器null和nolocal標識一個真值,它表示這個發佈者生成的消息不該該被它本身消費
TopicSubscriber subscriber = subSession.createSubscriber(chatTopic/*, null, true*/);

//設置一個JMS消息偵聽器
subscriber.setMessageListener(this);

//初始化Chat變量
this.connection = connection;
this.pubSession = pubSession;
this.username = username;
this.publisher = publisher;

//啓用JMS鏈接,容許發送消息
this.connection.start();
}

/**
* 接收來自TopicSubScriber的消息
*/

@Override
public void onMessage(Message message) {
try {
TextMessage textMessage = (TextMessage) message;
System.out.println(textMessage.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 使用發佈者發送消息
* writeMessage(這裏用一句話描述這個方法的做用)
*/
public void writeMessage(String text) throws JMSException{
TextMessage textMessage = pubSession.createTextMessage();
textMessage.setText(username+":"+text);
publisher.publish(textMessage);
}

/**
* 關閉聊天程序
*/
public void closeChat() throws JMSException{
connection.close();
}
}
函數

第五步:編寫測試類測試

package org.walden.chp02;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


import javax.jms.JMSException;
import javax.naming.NamingException;


public class TestChat1 {
public static void main(String[] args) throws IOException, NamingException,
JMSException {
Chat chat = new Chat(TestChat1.class.getName());
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
String text ="chat1:"+ reader.readLine();
if (text.endsWith("exit")) {
chat.closeChat();
System.exit(0);
} else {
chat.writeMessage(text);
}
}


}
}
this


package org.walden.chp02;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


import javax.jms.JMSException;
import javax.naming.NamingException;


public class TestChat2 {
public static void main(String[] args) throws NamingException, JMSException, IOException {
Chat chat = new Chat(TestChat2.class.getName());
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
String text ="chat2:"+ reader.readLine();
if (text.endsWith("exit")) {
chat.closeChat();
System.exit(0);
} else {
chat.writeMessage(text);
}
}
}
}
spa

相關文章
相關標籤/搜索