使用Activemq服务器STOMP协议连接android

时间:2014-04-08 11:57:43

标签: java android activemq stomp

我正在使用STOMP协议和Gozirra库连接机智的Activemq服务器。

这是我的代码:

package com.example.stomp;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.security.auth.login.LoginException;

import net.ser1.stomp.Client;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        String activeMqIp = "10.5.0.121";
        int port = 8161;

        String channel = "/topic/VirtualTopic.app.event.sensor.object.behavior.change";
        Client c;

        try {
            c = new Client(activeMqIp, port, "", "");
            Log.i("telo", "Client connected on " + activeMqIp + ":" + port);
            JavaStompClientSubscriberExample(c, channel);

        } catch (IOException ex) {
            ex.printStackTrace();

        } catch (LoginException ex) {
            ex.printStackTrace();
        }

    }

    public void JavaStompClientSubscriberExample(Client c, String channel) {
        Map header = new HashMap();
        header.put("transformation", "jms-object-xml");
        c.subscribe(channel, header);
        Log.i("telo", "Subscribed to " + channel);
    }

    public void message(Map map, String message) {
        Log.i("telo", "message received by STOMP listener\n" + message);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

这是我在logcat中的错误:

04-08 13:56:25.829: E/AndroidRuntime(10804): FATAL EXCEPTION: main
04-08 13:56:25.829: E/AndroidRuntime(10804): Process: com.example.stomp, PID: 10804
04-08 13:56:25.829: E/AndroidRuntime(10804): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.stomp/com.example.stomp.MainActivity}: android.os.NetworkOnMainThreadException
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.os.Looper.loop(Looper.java:136)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at java.lang.reflect.Method.invokeNative(Native Method)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at java.lang.reflect.Method.invoke(Method.java:515)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at dalvik.system.NativeStart.main(Native Method)
04-08 13:56:25.829: E/AndroidRuntime(10804): Caused by: android.os.NetworkOnMainThreadException
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at libcore.io.IoBridge.connect(IoBridge.java:112)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at java.net.Socket.startupSocket(Socket.java:567)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at java.net.Socket.tryAllAddresses(Socket.java:128)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at java.net.Socket.<init>(Socket.java:178)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at java.net.Socket.<init>(Socket.java:150)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at net.ser1.stomp.Client.<init>(Client.java:48)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at com.example.stomp.MainActivity.onCreate(MainActivity.java:40)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.app.Activity.performCreate(Activity.java:5231)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-08 13:56:25.829: E/AndroidRuntime(10804):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-08 13:56:25.829: E/AndroidRuntime(10804):    ... 11 more

我放入了Manifest互联网许可。我该如何解决这个错误?

由于

1 个答案:

答案 0 :(得分:0)

ActiveMQ使用端口 61613 进行Stomp协议。


以下是ActiveMQ支持的协议列表(带端口号)。 (在ActiveMQ服务器源代码&gt;&gt; activemq.xml文件中可用)。

<transportConnectors>
            <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>

使用与您的服务器配置相关的正确端口。您的客户端连接代码是正确的。