Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X
Hi,I'm going to write an expansion package to get some data, but in ThingWorx I made the following mistake:
[context: com.thingworx.webservices.context.HttpExecutionContext@4f87ac83][message: Unable to Invoke Service ReceiverStringMessage on TestHttpThing : org.jboss.netty.handler.codec.http.HttpRequest.setHeader(Ljava/lang/String;Ljava/lang/Object;)V],
ErrorCode(85ca1119-3609-4f9e-b926-c03164007877), Cause(org.jboss.netty.handler.codec.http.HttpRequest.setHeader(Ljava/lang/String;Ljava/lang/Object;)V), Reason(A Java method definition was not found at runtime.), Possible Resolutions([1]=Verify that you are compiling against and running against the same Jar and/or class definition versions., [2]=Verify that you have including the correct version of the Jar file in your Extension manifest file as a Jar Resource.)
java code:
package com.signalRTest;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.joda.time.DateTime;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
import com.ning.http.client.Response;
import com.thingworx.logging.LogUtilities;
import com.thingworx.metadata.annotations.ThingworxConfigurationTableDefinition;
import com.thingworx.metadata.annotations.ThingworxConfigurationTableDefinitions;
import com.thingworx.metadata.annotations.ThingworxDataShapeDefinition;
import com.thingworx.metadata.annotations.ThingworxFieldDefinition;
import com.thingworx.metadata.annotations.ThingworxServiceDefinition;
import com.thingworx.metadata.annotations.ThingworxServiceParameter;
import com.thingworx.metadata.annotations.ThingworxServiceResult;
import com.thingworx.things.Thing;
import com.thingworx.things.connected.IConnectedDevice;
@ThingworxConfigurationTableDefinitions(tables = {
@ThingworxConfigurationTableDefinition(name = "KafkaConnectionInfo", description = "Http Broker Connection Parameters", isMultiRow = false, ordinal = 0, dataShape = @ThingworxDataShapeDefinition(fields = {
@ThingworxFieldDefinition(ordinal = 0, name = "topic", description = "Http topic name", baseType = "STRING", aspects = {
"defaultValue:http", "friendlyName:Http topic name" }),
@ThingworxFieldDefinition(ordinal = 1, name = "serverAddress", description = "MQ broker serverAddress", baseType = "STRING", aspects = {
"defaultValue:127.0.0.1:9092", "friendlyName:Http serverAddress" }),
@ThingworxFieldDefinition(ordinal = 2, name = "userName", description = "Http broker username", baseType = "STRING", aspects = {
"defaultValue:username", "friendlyName:Http Username" }),
@ThingworxFieldDefinition(ordinal = 3, name = "password", description = "Http broker password", baseType = "PASSWORD", aspects = {
"defaultValue:", "friendlyName:Http Password" }),
@ThingworxFieldDefinition(ordinal = 4, name = "groupId", description = "Http groupId", baseType = "STRING", aspects = {
"defaultValue:groupid", "friendlyName:Http groupId" }),
@ThingworxFieldDefinition(ordinal = 5, name = "timeout", description = "Http timeout", baseType = "INTEGER", aspects = {
"defaultValue:6000", "friendlyName:Http timeout" }),
@ThingworxFieldDefinition(ordinal = 6, name = "ZookeeperSyncTimeMs", description = "Http ZookeeperSyncTimeMs", baseType = "INTEGER", aspects = {
"defaultValue:6000", "friendlyName:Http ZookeeperSyncTimeMs" }),
@ThingworxFieldDefinition(ordinal = 7, name = "retryInterval", description = "Http retryInterval", baseType = "INTEGER", aspects = {
"defaultValue:60000", "friendlyName:Http retryInterval" }) }) ) })
public class HttpReceiverThing extends Thing implements IConnectedDevice {
private static final long serialVersionUID = 1L;
protected static Logger logger = LogUtilities.getInstance().getApplicationLogger(HttpReceiverThing.class);
HttpConnectionEntity entity;
SignalRConnectUtil utils;
protected void initializeThing() throws Exception {
this.entity = new HttpConnectionEntity();
logger.info("开始初始化了接收数据initializeThing。。。。。。");
this.entity.setTopic(this.GetConfigurationTable("KafkaConnectionInfo").getRowValue("topic").toString());
this.entity.setGetServerAddress(
this.GetConfigurationTable("KafkaConnectionInfo").getRowValue("serverAddress").toString());
this.entity.setUserName(this.GetConfigurationTable("KafkaConnectionInfo").getRowValue("userName").toString());
this.entity.setPassword(this.GetConfigurationTable("KafkaConnectionInfo").getRowValue("password").toString());
this.entity.setGroupId(this.GetConfigurationTable("KafkaConnectionInfo").getRowValue("groupId").toString());
this.entity.setTimeout(
(Integer) this.GetConfigurationTable("KafkaConnectionInfo").getRowValue("timeout").getValue());
this.entity.setZookeeperSyncTimeMs(
(Integer) this.GetConfigurationTable("KafkaConnectionInfo").getRowValue("ZookeeperSyncTimeMs").getValue());
this.entity.setRetryInterval(
(Integer) this.GetConfigurationTable("KafkaConnectionInfo").getRowValue("retryInterval").getValue());
}
/**
* @desc 接收数据
* @param stringMessage
* @return
* @throws Exception
*/
@ThingworxServiceDefinition(name = "ReceiverStringMessage", description = "Receiver a string message to the specified Http")
@ThingworxServiceResult(name = "result", description = "", baseType = "STRING")
public String ReceiverStringMessage(){
// 1.创建网络请求对象
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
Future<Response> f;
Response r;
String token = "";
JSONObject jsonObject = null;
JSONArray jsonArray = null;
int status = 0;
String body="";
String groupid = "";
String url="";
String boxid = "";
String results = "";
try {
// 2.登录账号请求token
logger.error("开始登录");
BoundRequestBuilder b = asyncHttpClient
.preparePost("http://106.15.45.206:9010/core/connect/token");
logger.error("开始添加参数");
b.addParameter("username", "17721300797");
b.addParameter("password", "123456");
b.addParameter("client_id", "telecom_sdk");
b.addParameter("client_secret", "65d0122cf82946e8acd82a4fe1b290b4");
b.addParameter("grant_type", "password");
b.addParameter("scope", "openid offline_access fbox email profile");
logger.error("添加参数结束");
f = b.execute();
logger.error("开始执行登录");
r = f.get();
logger.error("登录成功");
status = r.getStatusCode();
body = r.getResponseBody();
logger.error("登录接口获取的body:"+body);
jsonObject = JSONObject.fromObject(body);
System.out.println("jsonObject:"+jsonObject);
if (status == 200) {
System.out.println("Login Succeed!");
token = jsonObject.getString("access_token");
System.out.println("token:"+token);
}
else{
System.out.println("Login failed");
}
status = r.getStatusCode();
// 获取盒子列表信息
b = asyncHttpClient
.prepareGet("http://106.15.45.206:9020/api/client/box/grouped");
b.addHeader("Authorization","Bearer "+token);
b.addHeader("X-FBox-ClientId", "12345678");
b.setBody("");
f = b.execute();
r = f.get();
status = r.getStatusCode();
body = r.getResponseBody();
results = body;
logger.error("盒子列表接口获取的body:"+body);
jsonArray = JSONArray.fromObject(body);
jsonObject=jsonArray.getJSONObject(0);
jsonArray = jsonObject.getJSONArray("boxRegs");
jsonObject = jsonArray.getJSONObject(0);
boxid = jsonObject.getString("boxUid");
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
asyncHttpClient.close();
asyncHttpClient=null;
return results;
}
@Override
public DateTime getLastConnectionTime() {
return null;
}
@Override
public boolean isConnected() {
return false;
}
/**
* 开启Thing
*/
protected void startThing() throws Exception {
logger.info("开启thing......");
if (this.utils == null) {
this.utils = new SignalRConnectUtil(this.entity);
}
}
/**
* 关闭Thing
*
* @throws Exception
*/
protected void stopThing() throws Exception {
logger.info("结束thing.......");
if (this.utils != null) {
// this.utils.disconnect();
}
}
}
metadata.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Entities>
<ExtensionPackages>
<ExtensionPackage dependsOn="" description="SignalRTest EXTENSIONS" minimumThingWorxVersion="7.4.0" name="signalRTestExtension" packageVersion="1.3.0" vendor="zb">
<JarResources>
<FileResource description="SignalRTest ThingPackage JAR" file="signalRTestExtension.jar" type="JAR"/>
<FileResource description="" file="httpclient.jar" type="JAR"/>
<FileResource description="" file="slf4j-log4j12-1.7.25.jar" type="JAR"/>
<FileResource description="" file="async-http-client-1.7.24.jar" type="JAR"/>
<FileResource description="" file="gson-2.3.jar" type="JAR"/>
<FileResource description="" file="java_websocket.jar" type="JAR"/>
<FileResource description="" file="json-lib-2.3-jdk15.jar" type="JAR"/>
<FileResource description="" file="signalr-client-sdk.jar" type="JAR"/>
<FileResource description="" file="slf4j-simple-1.7.13.jar" type="JAR"/>
<FileResource description="" file="commons-beanutils-1.8.0.jar" type="JAR"/>
<FileResource description="" file="ezmorph-1.0.4.jar" type="JAR"/>
</JarResources>
</ExtensionPackage>
</ExtensionPackages>
<ThingPackages>
<ThingPackage aspect.isCreatable="true" aspect.isSystemObject="false" className="com.signalRTest.HttpReceiverThing" description="" name="HttpReceiverThing">
<HandlerDefinitions/>
</ThingPackage>
</ThingPackages>
<ThingTemplates>
<ThingTemplate aspect.isSystemObject="false" description="" name="HttpReceiver" thingPackage="HttpReceiverThing">
<avatar/>
</ThingTemplate>
</ThingTemplates>
</Entities>
Thanks