Skip to main content

Red Hat OpenShift xPaaS - 在OpenShift上簡單的MQTT Demo, (二), 簡單的客戶端程式連接xPaaS上的訊息Broker

這個demo 我把它分成兩個部分,第一部分是在OpenShift上面建立一個有MQTT通訊協定的訊息的Broker。然後在第二部分就是寫一支簡單的小程式去連接雲端上的Broker.

這是第二部分的demo, 我們之前已經在OpenShift 上面成功地建立了一個訊息Broker. OpenShift 是紅帽的PaaS (平台即服務). 所以現在我們已經有個在雲端上服務的Broker拉!

MQTT 除了是個羽量級的通訊協定外,因為MQTT本身是走publish/subscribe 的傳輸模式,所以基本上是走Topic, 不會用到queue. 
這個demo 是個非常簡單的訊息產生,以及接收的小程式。透過MQTT 傳輸訊息,使用fusesource 的MQTT-Client 程式庫。

在開始這個部分前,我一定要不厭其煩地講,請記得把OpenShift 上面的設定設定好(請見第一部)。之後,我們就可以在本地的環境上設定跟xPaaS的部分。一般來說,雲端上的port是要給雲端上的替統連接的,如果要讓開發者可以從自己本機連接Cloud 那邊提供的服務,那不是很好嗎?Openshift 有個功能叫做port-forward, 可以讓你不用去TMD設定一堆有的沒有的網路設定,就會自動把在雲端的服務mapping 到本地的port. 設定上一點都不困難。如下:

rhc port-forward $YOUR_CONTAINER_NAME 






蘋果的使用者,有的時候你要建立一下alias, 把你openshift 的IP 連接到本地的機器上。

sudo ifconfig lo0 alias $OPENSHIFT_IP

現在開始要執行demo, 請先把整個demo複製到本機端,請執行:

git clone https://github.com/weimeilin79/mqtt-demo.git

執行init.sh 設定. 這個時候他會要求你輸入 Broker URL, Fuse console 使用者與密碼. 

./init.sh
  • broker URL 就是MQTT 的  port (如果你是照著demo做的話內建是 1883 ), IP 會對應到你的本地 .所以應該是127.0.0.1.
  • Fuse console 使用者與密碼都在先前建立broker時的就提供給你囉~







要跑小程式裡面的發送與接收端,請先到 projects/mqttdemo 的路徑下,依序執行

mvn -P consumer 
mvn -P producer  

這樣他就會透過MQTT 開始收送了
在同時,你openshift 上的broker 馬上就會有些統計的資料出現。



現在到開發這段,基本上,只要記得在pom.xml 中加入mqtt-client 程式庫就好

   
   
    org.fusesource.mqtt-client
    mqtt-client
    1.11-SNAPSHOT
   
   
    org.fusesource.mqtt-client
    mqtt-client-java1.4-uber
    1.11-SNAPSHOT
   
   
     org.fusesource.hawtbuf
    hawtbuf
    1.9
   
  

然後程式的部分真的就簡單到我都覺得汗顏,就只是幾個API 呼叫罷了
(好像還有一些比較好玩的API call, 等我有時間再寫囉.)


MQTT mqtt = new MQTT();
mqtt.setHost(props.getProperty(BROKER_URL));
mqtt.setUserName(props.getProperty(USERNAME));
mqtt.setPassword(props.getProperty(PASSWORD));
  
  
BlockingConnection connection = mqtt.blockingConnection();
connection.connect();
Topic[] topics = {new Topic(utf8("foo"), QoS.AT_LEAST_ONCE)};
connection.subscribe(topics);
Message message = connection.receive();
message.ack();
connection.disconnect();

/////VIDEO COMING SOON///////

Comments

Popular posts from this blog

Fuse Integration Service - Setup JBDS and create first quickstart application

Before we go and start creating our first application, I want to show you how to setup your JBoss Developer Studio, create a small application from the quickstart example and then running it on Fuse Integration Service.

I am using JBoss Developer Studio version 9, you can find it here.
After download the

jboss-devstudio-9.0.0.GA-installer-eap.jar
double-click it, and start installing with default values.

After successful installation, we will need install the plugins for Fuse, on JBoss Central view, select software update, select enable early access.


And select JBoss Fuse Development for the plugin,


Click on install, and we are all set to go!

First thing first, we want to create a Fuse project to deploy on the base of Fuse Integration Service, which is OpenShift. If you have not installed it, please go back to my previous post for instructions. So on your JBDS, right click and start creating the project. Select new, maven project, if you have installed the plugin correctly, you should …

Red Hat JBoss Fuse/A-MQ - Fuse and A-MQ Version 6.3 GA is released!

Fuse and A-MQ 6.3 GA has just went out. Maybe, you would think this is just only a minor version release why should I care? Hold your thoughts on that! Because they have done a lot of improvements and also added many new features into this release.

Besides various bug fixes and making sure Fuse Fabric is much more stable. There are two major change in this version update:

New Tooling in JBoss Developer Studio (JBDS) 9.1 GA. Newer Apache Camel version – Camel v2.17. I was really impressed by the work put in to make developing Camel application much simpler. First is the installation of tooling itself. Now it has a all-in-one installer so you don't need to worry about which plugins you need to check. See the videos below to see the new "Getting Started" of Fuse 6.3.



And If you notice from the above video, the presentation of camel route in JBDS has also updated. It fixed some of the miss representation of logic and making it easier to read.

Old Camel Route
New Camel Route
On …

RHTE - Supercharge your integration services

Red Hat Tech Exchange has taken place in Vietnam, Ho Chi Minh city two weeks ago, it is a great event held by Red Hat in Asia Pacific Region. It is open to all Red Hat partners who are interested in learning what Red Hat is doing recently, see what the trend of the open source world, basically it is a great event to share your knowledge and experience, to meet other enthusiastic people.

I am very fortunate to talk in this great event, to talk about the things I have been working on and even discuss it with many. Also got lots of great ideas too. So here are the slide.

My first talk was with Thomas Qvarnström about how to handle large size data in JBoss Fuse and how JBoss Data Grid can help in the situation.

Here is the agenda of the talk, we will be talk about this in the up coming webinar on 24th Sept.

Integration often involves storing, retrieving, and transforming data. Using a traditional database in your integration is likely to becomes a bottleneck that is expensive and hard to …