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

 0"/>

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);

	}

 

 

你可能感兴趣的:(activemq,android推送,push,mqtt客户端,mqtt推送)