Use the DataService directly. Ask OSGi to inject the instance into your component.
Sample code to use in your component class:
public class MyComponent {
private DataService m_dataService;
public void setDataService(DataService dataService) {
m_dataService = dataService;
}
public void unsetDataService(DataService dataService) {
m_dataService = null;
}
// activate() deactivate() and all required methods
public void publish() {
String topic = "your/topic";
String payload = "Hello!";
int qos = 0;
boolean retain = false;
try {
m_dataService.publish(topic, payload.getBytes(), qos, retain, 2);
s_logger.info("Publish ok");
} catch (Exception e) {
s_logger.error("Error while publishing", e);
}
}
}
In your component OSGI-INF/mycomponent.xml tell OSGi which methods to call to inject the DataService by adding the following
<reference name="DataService"
interface="org.eclipse.kura.data.DataService"
bind="setDataService"
unbind="unsetDataService"
cardinality="1..1"
policy="static" />
Then you can pass the topic you need to DataService.publish(...)
. Payloads must be converted to byte[]
arrays.