└── cloudAMQP
├── .classpath
├── .project
├── AndroidManifest.xml
├── proguard-project.txt
├── project.properties
├── res
├── drawable-hdpi
│ └── ic_launcher.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ └── ic_launcher.png
├── drawable-xxhdpi
│ └── ic_launcher.png
├── layout
│ └── activity_main.xml
├── menu
│ └── main.xml
├── values-w820dp
│ └── dimens.xml
└── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
└── src
└── com
└── cloudamqp
└── androidexample
└── ActivityHome.java
/cloudAMQP/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/cloudAMQP/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | cloudAMQP
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/cloudAMQP/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
9 |
15 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/cloudAMQP/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/cloudAMQP/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-20
15 | android.library.reference.1=../appcompat_v7
16 |
--------------------------------------------------------------------------------
/cloudAMQP/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cloudamqp/android-example/848edca38feb287f7d9573c8d960746efc7dc9b8/cloudAMQP/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/cloudAMQP/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cloudamqp/android-example/848edca38feb287f7d9573c8d960746efc7dc9b8/cloudAMQP/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/cloudAMQP/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cloudamqp/android-example/848edca38feb287f7d9573c8d960746efc7dc9b8/cloudAMQP/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/cloudAMQP/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cloudamqp/android-example/848edca38feb287f7d9573c8d960746efc7dc9b8/cloudAMQP/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/cloudAMQP/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
10 |
11 |
15 |
16 |
22 |
23 |
29 |
30 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/cloudAMQP/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
13 |
--------------------------------------------------------------------------------
/cloudAMQP/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 | 64dp
9 |
10 |
11 |
--------------------------------------------------------------------------------
/cloudAMQP/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/cloudAMQP/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | cloudAMQP
5 | Hello cloudAMQP
6 | Settings
7 |
8 |
9 |
--------------------------------------------------------------------------------
/cloudAMQP/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/cloudAMQP/src/com/cloudamqp/androidexample/ActivityHome.java:
--------------------------------------------------------------------------------
1 | package com.cloudamqp.androidexample;
2 | import java.net.URISyntaxException;
3 | import java.security.KeyManagementException;
4 | import java.security.NoSuchAlgorithmException;
5 | import java.text.SimpleDateFormat;
6 | import java.util.Date;
7 | import java.util.concurrent.BlockingDeque;
8 | import java.util.concurrent.LinkedBlockingDeque;
9 |
10 | import com.cloudamqp.R;
11 | import com.rabbitmq.client.Channel;
12 | import com.rabbitmq.client.Connection;
13 | import com.rabbitmq.client.ConnectionFactory;
14 | import com.rabbitmq.client.QueueingConsumer;
15 | import com.rabbitmq.client.AMQP.Queue.DeclareOk;
16 |
17 | import android.app.Activity;
18 |
19 | import android.os.Bundle;
20 | import android.os.Handler;
21 | import android.os.Message;
22 | import android.util.Log;
23 | import android.view.View;
24 | import android.view.View.OnClickListener;
25 | import android.widget.Button;
26 | import android.widget.EditText;
27 | import android.widget.TextView;
28 |
29 | public class ActivityHome extends Activity {
30 | @Override
31 | public void onCreate(Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_main);
34 |
35 | setupConnectionFactory();
36 | publishToAMQP();
37 | setupPubButton();
38 |
39 | final Handler incomingMessageHandler = new Handler() {
40 | @Override
41 | public void handleMessage(Message msg) {
42 | String message = msg.getData().getString("msg");
43 | TextView tv = (TextView) findViewById(R.id.textView);
44 | Date now = new Date();
45 | SimpleDateFormat ft = new SimpleDateFormat ("hh:mm:ss");
46 | tv.append(ft.format(now) + ' ' + message + '\n');
47 | }
48 | };
49 | subscribe(incomingMessageHandler);
50 | }
51 |
52 | void setupPubButton() {
53 | Button button = (Button) findViewById(R.id.publish);
54 | button.setOnClickListener(new OnClickListener() {
55 | @Override
56 | public void onClick(View arg0) {
57 | EditText et = (EditText) findViewById(R.id.text);
58 | publishMessage(et.getText().toString());
59 | et.setText("");
60 | }
61 | });
62 | }
63 |
64 | Thread subscribeThread;
65 | Thread publishThread;
66 | @Override
67 | protected void onDestroy() {
68 | super.onDestroy();
69 | publishThread.interrupt();
70 | subscribeThread.interrupt();
71 | }
72 |
73 | private BlockingDeque queue = new LinkedBlockingDeque();
74 | void publishMessage(String message) {
75 | //Adds a message to internal blocking queue
76 | try {
77 | Log.d("","[q] " + message);
78 | queue.putLast(message);
79 | } catch (InterruptedException e) {
80 | e.printStackTrace();
81 | }
82 | }
83 |
84 | ConnectionFactory factory = new ConnectionFactory();
85 | private void setupConnectionFactory() {
86 | String uri = "CLOUDAMQP_URL";
87 | try {
88 | factory.setAutomaticRecoveryEnabled(false);
89 | factory.setUri(uri);
90 | } catch (KeyManagementException | NoSuchAlgorithmException | URISyntaxException e1) {
91 | e1.printStackTrace();
92 | }
93 | }
94 |
95 | void subscribe(final Handler handler)
96 | {
97 | subscribeThread = new Thread(new Runnable() {
98 | @Override
99 | public void run() {
100 | while(true) {
101 | try {
102 | Connection connection = factory.newConnection();
103 | Channel channel = connection.createChannel();
104 | channel.basicQos(1);
105 | DeclareOk q = channel.queueDeclare();
106 | channel.queueBind(q.getQueue(), "amq.fanout", "chat");
107 | QueueingConsumer consumer = new QueueingConsumer(channel);
108 | channel.basicConsume(q.getQueue(), true, consumer);
109 |
110 | // Process deliveries
111 | while (true) {
112 | QueueingConsumer.Delivery delivery = consumer.nextDelivery();
113 |
114 | String message = new String(delivery.getBody());
115 | Log.d("","[r] " + message);
116 |
117 | Message msg = handler.obtainMessage();
118 | Bundle bundle = new Bundle();
119 |
120 | bundle.putString("msg", message);
121 | msg.setData(bundle);
122 | handler.sendMessage(msg);
123 | }
124 | } catch (InterruptedException e) {
125 | break;
126 | } catch (Exception e1) {
127 | Log.d("", "Connection broken: " + e1.getClass().getName());
128 | try {
129 | Thread.sleep(4000); //sleep and then try again
130 | } catch (InterruptedException e) {
131 | break;
132 | }
133 | }
134 | }
135 | }
136 | });
137 | subscribeThread.start();
138 | }
139 |
140 | public void publishToAMQP()
141 | {
142 | publishThread = new Thread(new Runnable() {
143 | @Override
144 | public void run() {
145 | while(true) {
146 | try {
147 | Connection connection = factory.newConnection();
148 | Channel ch = connection.createChannel();
149 | ch.confirmSelect();
150 |
151 | while (true) {
152 | String message = queue.takeFirst();
153 | try{
154 | ch.basicPublish("amq.fanout", "chat", null, message.getBytes());
155 | Log.d("", "[s] " + message);
156 | ch.waitForConfirmsOrDie();
157 | } catch (Exception e){
158 | Log.d("","[f] " + message);
159 | queue.putFirst(message);
160 | throw e;
161 | }
162 | }
163 | } catch (InterruptedException e) {
164 | break;
165 | } catch (Exception e) {
166 | Log.d("", "Connection broken: " + e.getClass().getName());
167 | try {
168 | Thread.sleep(5000); //sleep and then try again
169 | } catch (InterruptedException e1) {
170 | break;
171 | }
172 | }
173 | }
174 | }
175 | });
176 | publishThread.start();
177 | }
178 | }
179 |
--------------------------------------------------------------------------------