`

activeMQ 推送之mqtt客户端2

阅读更多

使用activeMQ进行android推送时需要如下问题

(1)activeMQ后台报错:Frame size of 257 MB larger than max allowed 100 MB

详细错误信息:

WARN  | Transport Connection to: tcp://127.0.0.1:50916 failed: java.io.IOException: Frame size of 257 MB larger than max allowed 100 MB | org.apache.activemq.broker.TransportConnection.Transport | ActiveMQ Transport: tcp:///127.0.0.1:50916@61616

 解决方法:

修改配置文件apache-activemq-5.9.0-bin\apache-activemq-5.9.0\conf\activemq.xml

 <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=1048576000"/>

maxFrameSize 调大一些.

(2)mqtt 客户端如何设置用户名和密码

/***
	 * 客户端和activeMQ服务器建立连接
	 * @param BROKER_URL
	 * @param clientId : 用于标识客户端,相当于ios中的device token
	 * @param TOPIC
	 * @param isCleanSession :false--可以接受离线消息;
	 * @return 是否启动成功 
	 */
	private boolean connect(String BROKER_URL,String clientId,String TOPIC,boolean isCleanSession){
		try {
			ComponentUtil.appendResult(resultTextPane, "connect time:"+TimeHWUtil.getCurrentMiniuteSecond(), true);
            mqttClient = new MqttClient(BROKER_URL, clientId, new MemoryPersistence());
            MqttConnectOptions options= new MqttConnectOptions();
            options.setCleanSession(isCleanSession);//mqtt receive offline message
            ComponentUtil.appendResult(resultTextPane, "isCleanSession:"+isCleanSession, true);
            options.setKeepAliveInterval(30);
            String username=usernameTextField.getText();
            String password=passwordTextField.getText();
            if(ValueWidget.isNullOrEmpty(username)){
            	username=null;
            }
            if(ValueWidget.isNullOrEmpty(password)){
            	password=null;
            }else{
            	options.setPassword(password.toCharArray());
            }
            options.setUserName(username);
            
            //推送回调类,在此类中处理消息,用于消息监听
            mqttClient.setCallback(new MyCallBack(MqttClientSwing.this));
            boolean isSuccess=false;
            try {
				mqttClient.connect(options);//CLIENT ID CAN NOT BE SAME
				isSuccess=true;
			} catch (Exception e) {
				if(isPrintException){
					e.printStackTrace();
				}
			}
            if(!isSuccess){
            	String message="连接失败,请检查client id是否重复了 或者activeMQ是否启动";
            	ComponentUtil.appendResult(resultTextPane, message, true);
            	GUIUtil23.warningDialog(message);
            	return false;
            }else{
            //Subscribe to topics 
	            mqttClient.subscribe(new String[]{TOPIC,clientId});
	           
	            System.out.println("topic:"+TOPIC+",  "+(clientId));
	            ComponentUtil.appendResult(resultTextPane, "TOPIC:"+TOPIC+",  "+(clientId), true);
            }

        } catch (MqttException e) {
        	if(isPrintException){
            e.printStackTrace();}
            GUIUtil23.errorDialog(e.getMessage());
            return false;
        }
		return true;
	}

 

(3)发布者如何设置密码

/**
	 * 初始化connection和session
	 * 
	 * @throws Exception
	 */
	private void init(/* String mqIp,boolean transacted */) throws Exception {
		if (!DialogUtil.verifyTFEmpty(serverIpTextField, "服务器ip")) {
			return;
		}
		String transactedStr = transactedTextField.getText();
		boolean transacted = false;
		if (ValueWidget.isNullOrEmpty(transactedStr)) {
			transacted = false;
		} else {
			transacted = Boolean.parseBoolean(transactedStr);
		}
		String message = "transacted:" + transacted;
		ComponentUtil.appendResult(resultTextArea, message, false);
		// System.out.println(message);
		String brokerUrl = String.format(BROKER_URL,
				serverIpTextField.getText());
		
		 String username=usernameTextField.getText();
         String password=passwordTextField.getText();
         if(ValueWidget.isNullOrEmpty(username)){
         	username=null;
         }
         if(ValueWidget.isNullOrEmpty(password)){
         	password=null;
         }
         
		// 创建链接工厂
		TopicConnectionFactory factory = new ActiveMQConnectionFactory(
				username,
				password, brokerUrl);
		ComponentUtil.appendResult(resultTextArea, "activeMQ url:" + brokerUrl,
				true);
		// 通过工厂创建一个连接
		connection = factory.createTopicConnection();
		// 启动连接
		connection.start();
		ComponentUtil.appendResult(resultTextArea, "启动connection 成功", true);
		// 创建一个session会话 transacted
		session = connection.createTopicSession(
				transacted /* Boolean.FALSE */, Session.AUTO_ACKNOWLEDGE);

	}

 

 

1
1
分享到:
评论
7 楼 Yunba云巴 2016-12-13  
我们云巴https://yunba.io/也是基于MQTT协议实现的消息推送功能呢,当然不仅仅局限于Android推送,同样也拥有iOS推送功能。

对于Android推送来说,客户端集成了我们云巴https://yunba.io/的 Android SDK后,服务端可通过 SDK 或使用 RESTful API,向 Android 客户端发消息。在提高消息的实时性以及到达率方面,我们采取的措施是启动一个后台的 Service,创建并保持到服务器的长连接,以及支持离线消息。

除此以外,我们还一键集成第三方推送(目前是华为、小米)功能,最大程度保证到达率。
6 楼 hw1287789687 2014-10-10  
5 楼 hw1287789687 2014-10-10  
缺少jar包,把activemq-all-x.x.jar 添加到classpath 里
同时,检查一下java运行级别,运行级别应该是java 1.6
4 楼 Bigfeetliu 2014-10-10  
连不上,报错!貌似是TopicConnectionFactory类的问题,不知道如何解决啊


Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: javax/management/j2ee/statistics/Stats
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.mqtt.hw.pusher.PusherApp.init(PusherApp.java:117)
at com.mqtt.hw.pusher.PusherApp.access$7(PusherApp.java:100)
at com.mqtt.hw.pusher.PusherApp$5.actionPerformed(PusherApp.java:306)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.management.j2ee.statistics.Stats
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 51 more
3 楼 hw1287789687 2014-09-30  
jackyx 写道
提示无法连接到服务器,已确定服务器端已经存在,并且服务器端的管理网页可以打开,服务也正常启动,是否在客户端连接的时候的URL有问题还是?

参考:
http://hw1287789687.iteye.com/blog/2067736
http://www.yunmasoft.com
2 楼 jackyx 2014-09-29  
提示无法连接到服务器,已确定服务器端已经存在,并且服务器端的管理网页可以打开,服务也正常启动,是否在客户端连接的时候的URL有问题还是?
1 楼 jackyx 2014-09-29  
这个我试下了,没显示连接成功还是失败

相关推荐

Global site tag (gtag.js) - Google Analytics