Exchange messages on JADEX 2.4 - java

I have to make a work with BDI Agents and for that i will use JADEX 2.4 but i have a big problem. The documentation is a bit poor and i can't exchange messages between agents.
I have read this article http://www.activecomponents.org/bin/view/AC+Tutorial/05+Provided+Services
And i'm trying make the same thing on my code but no success. I need to know how to do 2 things for make my work: send a message from one agent to other, and send a message from one agent to all agents. Anyone knows how to do that?
The code that i have is the following:
ChatSystem.java
package agents;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import ....
#Service
public class ChatSystem implements IChatService{
#ServiceComponent
protected IInternalAccess agent;
protected IClockService clock;
protected DateFormat format;
#ServiceStart
public IFuture<IClockService> startService(){
format = new SimpleDateFormat("hh:mm:ss");
final Future<IClockService> ret = new Future<IClockService>();
IFuture<IClockService> fut = agent.getServiceContainer().getRequiredService("clockservice");
fut.addResultListener(new DelegationResultListener<IClockService>(ret)
{
public void customResultAvailable(IClockService result)
{
clock = result;
super.customResultAvailable(null);
}
});
return ret;
}
#Override
public IFuture<Void> message(String nick, String text,
boolean privatemessage) {
// TODO Auto-generated method stub
//System.out.println(" received at" + text);
System.out.println(agent.getComponentIdentifier().getLocalName()+" received at "
+" from: "+nick+" message: "+text);
return null;
}
}
HelperAgent.java
package agents;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import .....
#Agent
#Service
#RequiredServices({#RequiredService(name="clockservice", type=IClockService.class,binding=#Binding(scope=RequiredServiceInfo.SCOPE_PLATFORM)),#RequiredService(name="chatservices", type=IClockService.class,binding=#Binding(scope=RequiredServiceInfo.SCOPE_PLATFORM,dynamic=true),multiple=true)})
#ProvidedServices(#ProvidedService(type=IChatService.class, implementation=#Implementation(ChatSystem.class)))
public class HelperAgent {
#Agent
protected MicroAgent agent;
#AgentBody
public void AgentBody()
{
IFuture<IClockService> fut = agent.getServiceContainer().getRequiredService("clockservice");
fut.addResultListener(new DefaultResultListener<IClockService>()
{
public void resultAvailable(IClockService cs)
{
System.out.println("Time for a chat, buddy: "+new Date(cs.getTime()));
}
});
IFuture<Collection<IChatService>> chatservices = agent.getServiceContainer().getRequiredServices("chatservices");
chatservices.addResultListener(new DefaultResultListener<Collection<IChatService>>()
{
public void resultAvailable(Collection<IChatService> result)
{
for(Iterator<IChatService> it=result.iterator(); it.hasNext(); )
{
IChatService cs = it.next();
cs.message(agent.getComponentIdentifier().getName(), "test",false);
}
}
});
}
}
Anyone can help me?
Regards

In Jadex you work with active components representing enhanced agents, i.e. in addition to sending and receiving messages you can work with services. Agents can expose services using Java interfaces and other agents can simply fetch these services via their type. Using services communication is done without having to know agent identifities. This helps in building more SOA driven solutions dynamic service providers.
If you want to communicate via messages the API depends on the type of component you are using. In case of micro agents (as shown in your snippets) you can just prepare a FIPA message and call sendMessage on the agent API as shown below:
Map msg = new HashMap();
msg.put(SFipa.CONTENT, content);
msg.put(SFipa.PERFORMATIVE, SFipa.QUERY_IF);
msg.put(SFipa.CONVERSATION_ID, "someuniqueid");
msg.put(SFipa.RECEIVERS, new IComponentIdentifier[]{receiver});
msg.put(SFipa.SENDER, getComponentIdentifier());
agent.sendMessage(msg, SFipa.FIPA_MESSAGE_TYPE);
with 'agent' being the injected MicroAgent.
Kind regards
Lars

Related

How to get the playercount from another Paper Spigot server running through Bungeecord

I'm currently working on a plugin for Bukkit (Paper Spigot) that acts as a queue. This plugin in running in a queue server that then will take players over to the proper server when the proper server has less that a set number of players. I need to obtain the playercount from the proper server and use it in code from my queue plugin. I found examples on how to do this using Bukkit's messaging channel but that requires players (I think) and I don't completely understand it even though I've read many other articles and stackoverflow posts as most of these people have had slightly different problems. I've also heard other people talk about sockets but I'm not sure how they work. I am fine with running a second plugin in either the proper server or the Bungeecord server. I just need to know what code to write and where to write it! or at least a helpful example of a working system.
Here's my code so far:
package com.Package;
import org.bukkit.Bukkit;
//import org.bukkit.command.Command;
//import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.PluginMessageListener;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import java.util.ArrayList;
import java.util.logging.Logger;
public class AnarchyQueue extends JavaPlugin implements PluginMessageListener {
ArrayList<Player> players = new ArrayList<Player>();
Boolean isListening = false;
int playerNumber = 0;
#Override
public void onEnable() {
getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run() {
getCount();
log(Integer.toString(playerNumber));
}
}, 200L, 200L);
}
#Override
public void onDisable() {
}
private void log(String str)
{
Logger.getLogger("Minecraft").info(str);
}
public void onPlayerJoin(PlayerJoinEvent event)
{
players.add(event.getPlayer());
}
/*public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (command.getName().equalsIgnoreCase("players")) {
if (!(sender instanceof Player)) {
sender.sendMessage("You must be in game!");
return true;
}
else {
getCount();
}
}
return true;
}*/
private void getCount() {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF("PlayerCount");
out.writeUTF("anarchy");
Player player = Bukkit.getPlayerExact("Ed_Silver");
player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
isListening=true;
}
#Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (channel.equals("BungeeCord")) {
ByteArrayDataInput in = ByteStreams.newDataInput(message);
String subchannel = in.readUTF();
if (subchannel.equals("PlayerCount") && isListening) {
int playerCount = in.readInt();
playerNumber = playerCount;
isListening=false;
}
}
}
}
I know this code is broken in many ways, I am not really looking to fix it - I just want a way to do what I'm doing and I'll work the rest out later. (giving code as people tend to get shirty if they think you haven't tried :) !)
Feel free to ask for more detail etc...
Thanks in advance,
Edward
--EDIT--
I have found Jedis and have been experimenting. If someone could explain Jedis to me that would be great.
This is what I've found so far:
public static void main(String[] args) {// priOnEnable() {
//Connecting to Redis server on localhost
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
//check whether server is running or not
System.out.println("Server is running: "+jedis.ping());
}
This gets an error however. I believe that I need to be running something else to manage Redis or something.
Thanks Again,
Edward
What I've done:
Installed Redis (https://github.com/MicrosoftArchive/redis/releases)
Used guides such as https://www.tutorialspoint.com/redis/redis_java.htm to work out code.
Good luck to all those out there attempting this!
Have fun and I hope you work it out!

Grpc.Core.RpcException method is unimplemented with C# client and Java Server

I am having trouble finding the source of this error. I implemented a simple service using protobuf:
syntax = "proto3";
package tourism;
service RemoteService {
rpc Login(LoginUserDTO) returns (Response) {}
}
message AgencyDTO{
int32 id=1;
string name=2;
string email=3;
string password=4;
}
message LoginUserDTO{
string password=1;
string email=2;
}
message SearchAttractionsDTO{
string name=1;
int32 start_hour=2;
int32 start_minute=3;
int32 stop_hour=4;
int32 stop_minute=5;
AgencyDTO loggedUser=6;
}
message AttractionDTO{
int32 id=1;
string name=2;
string agency=3;
int32 hour=4;
int32 minute=5;
int32 seats=6;
int32 price=7;
}
message ReservationDTO{
int32 id=1;
string first_name=2;
string last_name=3;
string phone=4;
int32 seats=5;
AttractionDTO attraction=6;
AgencyDTO agency=7;
}
message Response{
enum ResponseType{
OK=0;
NOT_LOGGED_ID=1;
SERVER_ERROR=2;
VALIDATOR_ERROR=3;
}
ResponseType type=1;
AgencyDTO user=2;
string message=3;
}
When using a java client everything works fine, the server receives the request and responds appropriately. When using C# with the same .proto file for generating sources at the client.Login() I get the following errror: Grpc.Core.RpcException Status(StatusCode=Unimplemented, Detail="Method tourism.RemoteService/Login is unimplemented"). The server receives the request but does not have time to respond and throws:
INFO: Request from ex#ex.com
May 22, 2017 12:28:58 AM io.grpc.internal.SerializingExecutor run
SEVERE: Exception while executing runnable io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$2#4be43082
java.lang.IllegalStateException: call is closed
at com.google.common.base.Preconditions.checkState(Preconditions.java:174)
at io.grpc.internal.ServerCallImpl.sendHeaders(ServerCallImpl.java:103)
at io.grpc.stub.ServerCalls$ServerCallStreamObserverImpl.onNext(ServerCalls.java:282)
at ServiceImp.login(ServiceImp.java:20)
at tourism.RemoteServiceGrpc$MethodHandlers.invoke(RemoteServiceGrpc.java:187)
at io.grpc.stub.ServerCalls$1$1.onHalfClose(ServerCalls.java:148)
at io.grpc.internal.ServerCallImpl$ServerStreamListenerImpl.halfClosed(ServerCallImpl.java:262)
at io.grpc.internal.ServerImpl$JumpToApplicationThreadServerStreamListener$2.runInContext(ServerImpl.java:572)
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:52)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:117)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Java server:
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import tourism.RemoteServiceGrpc;
import tourism.Service;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by Andu on 21/05/2017.
*/
public class ServerGrpc {
Logger logger= Logger.getLogger(ServerGrpc.class.getName());
private final Server server;
private final int port;
public ServerGrpc(int p){
port=p;
server= ServerBuilder.forPort(port).addService(new ServiceImp()).build();
}
public void start() throws IOException {
server.start();
logger.info("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
// Use stderr here since the logger may has been reset by its JVM shutdown hook.
System.err.println("*** shutting down gRPC server since JVM is shutting down");
ServerGrpc.this.stop();
System.err.println("*** server shut down");
}
});
}
public void stop() {
if (server != null) {
server.shutdown();
}
}
void blockUntilShutdown() throws InterruptedException {
if (server != null) {
server.awaitTermination();
}
}
private class ServiceImp extends RemoteServiceGrpc.RemoteServiceImplBase {
Logger log=Logger.getLogger(ServiceImp.class.getName());
#Override
public void login(Service.LoginUserDTO request, StreamObserver<Service.Response> responseStreamObserver){
super.login(request,responseStreamObserver);
log.log(Level.INFO,"Request from "+request.getEmail());
Service.Response response= Service.Response.newBuilder().setMessage("Hello "+request.getEmail()+", I know your password: "+request.getPassword()).build();
responseStreamObserver.onNext(response);
responseStreamObserver.onCompleted();
}
}
}
C# Client:
namespace testGrpc2
{
class MainClass
{
public static void Main(string[] args)
{
var channel = new Channel("127.0.0.1:61666",ChannelCredentials.Insecure);
var client = new RemoteService.RemoteServiceClient(channel);
Response response=client.Login(new LoginUserDTO{Email="ex#ex.com",Password="notmypassword"});
Console.WriteLine(response);
Console.ReadKey();
}
}
}
I managed to find the source of the problem. For anyone else having this problem:
Make sure your .proto file is identical for both client and server and it has the same package. When the client calls a method on the remote server, it uses the full name of the remote class and the package.
However this was not the reason why the method appeared as unimplemented to the client. It was this:
super.login(request,responseStreamObserver);
Calling the super method login sends an async UNIMPLEMENTED error code back to the client. This is the login() method in the generated class:
public void login(LoginUserDTO request,StreamObserver<Response> responseObserver) {
asyncUnimplementedUnaryCall(METHOD_LOGIN, responseObserver);
}
So make sure in the implementation of your service methods you don't call the super method as it will appear to the client as UNIMPLEMENTED. If you generate #Override methods using IntelliJ IDEA it will add the super method call. Make sure to delete it.
For me it was that I forget adding endpoint of gRpc service in startup class.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<GreeterService>();
//Add your endpoint here like this
endpoints.MapGrpcService<YourProtoService>();
});
For me, using the C# client, the problem was that I wasn't overriding the generated service method.
Thanks to Alexandru - this helped solve my problem with Akka gRPC client and Python grPC server. In my case, I had same packages and preamble in .proto file but had eliminated message classes and gRPC functions not needed for this specific use case in the Python gRPC server. When I made the .proto files identical, everything worked and I no longer received UNIMPLEMENTED errors. This is needed for languages beyond the C#/Java example cited. Thanks again.
My server and client were both java, and the problem was removed after closing and opening the project in IntelliJ!!

How to get the exception that was thrown when a Cucumber test failed in Java?

I can perform actions on test failure by using:
#After
public void afterTest(Scenario scenario) {
if (scenario.isFailed()) {
/*Do stuff*/
}
}
However some of the actions I need to perform depend on the Exception that was thrown and in what context it was thrown. Is there a way to get the Throwable that caused the test to fail? For example in JUnit I would do this by extending TestWatcher and add a rule to my tests:
#Override
protected void failed(Throwable e, Description description) {
/*Do stuff with e*/
}
However the cucumber-junit iplementation does not allow the use of rules, so this solution would not work with Cucumber.
I don't think I need to explain why accessing a thrown exception on test failure would be useful, however I will still provide an Example:
My test environment is not always stable, so my tests might fail unexpectedly at any moment (there's no specific place I can try to catch the exception since it could occur at any time). When this happens I need the test to reschedule for another attempt, and log the incident so that we can get some good statistical data on the environment instability (when, how frequent, how long etc.)
The problem with the work around suggested by Frank Escobar:
By using reflection to reach into a frameworks internals you're depending on implementation details. This is a bad practice, when ever the framework changes its implementation your code may break as you will observe in Cucumber v5.0.0.
Hooks in Cucumber are designed to manipulate the test execution context before and after a scenario. They're not made to report on the test execution itself. Reporting is cross cutting concern and best managed by using the plugin system.
For example:
package com.example;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.Result;
import io.cucumber.plugin.event.Status;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestCaseFinished;
public class MyTestListener implements ConcurrentEventListener {
#Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestCaseFinished.class, this::handleTestCaseFinished);
}
private void handleTestCaseFinished(TestCaseFinished event) {
TestCase testCase = event.getTestCase();
Result result = event.getResult();
Status status = result.getStatus();
Throwable error = result.getError();
String scenarioName = testCase.getName();
String id = "" + testCase.getUri() + testCase.getLine();
System.out.println("Testcase " + id + " - " + status.name());
}
}
When using JUnit 4 and TestNG you can activate this plugin using:
#CucumberOptions(plugin="com.example.MyTestListener")
With JUnit 5 you add it to junit-platform.properties:
cucumber.plugin=com.example.MyTestListener
Or if you are using the CLI
--plugin com.example.MyTestListener
I've implemented this method using reflections. You can't access directly to steps errors (stack trace). I've created this static method which allows you to access to "stepResults" attribute and then you can iterate and get the error and do whatever you want.
import cucumber.runtime.ScenarioImpl;
import gherkin.formatter.model.Result;
import org.apache.commons.lang3.reflect.FieldUtils;
import java.lang.reflect.Field;
import java.util.ArrayList;
#After
public void afterScenario(Scenario scenario) {
if (scenario.isFailed())
logError(scenario);
}
private static void logError(Scenario scenario) {
Field field = FieldUtils.getField(((ScenarioImpl) scenario).getClass(), "stepResults", true);
field.setAccessible(true);
try {
ArrayList<Result> results = (ArrayList<Result>) field.get(scenario);
for (Result result : results) {
if (result.getError() != null)
LOGGER.error("Error Scenario: {}", scenario.getId(), result.getError());
}
} catch (Exception e) {
LOGGER.error("Error while logging error", e);
}
}
You can to this by writing your own custom implementation of Formatter & Reporter interface. The empty implementation of Formatter is the NullFormatter.java which you can extend. You will need to provide implementations for the Reporter interface.
The methods which would be of interest will be the result() of the Reporter interface and possibly the done() method of Formatter. The result() has the Result object which has the exceptions.
You can look at RerunFormatter.java for clarity.
Github Formatter source
public void result(Result result) {
//Code to create logs or store to a database etc...
result.getError();
result.getErrorMessage();
}
You will need to add this class(com.myimpl.CustomFormRep) to the plugin option.
plugin={"pretty", "html:report", "json:reports.json","rerun:target/rerun.txt",com.myimpl.CustomFormRep}
More details on custom formatters.
You can use the rerun plugin to get a list of failed scenarios to run again. Not sure about scheduling a run of failed tests, code to create a batch job or schedule one on your CI tool.
This is the workaround for cucumber-java version 4.8.0 using reflection.
import cucumber.api.Result;
import io.cucumber.core.api.Scenario;
import io.cucumber.core.logging.Logger;
import io.cucumber.core.logging.LoggerFactory;
import io.cucumber.java.After;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
#After
public void afterScenario(Scenario scenario) throws IOException {
if(!scenario.getStatus().isOk(true)){
logError(scenario);
}
}
private static void logError(Scenario scenario) {
try {
Class clasz = ClassUtils.getClass("cucumber.runtime.java.JavaHookDefinition$ScenarioAdaptor");
Field fieldScenario = FieldUtils.getField(clasz, "scenario", true);
fieldScenario.setAccessible(true);
Object objectScenario = fieldScenario.get(scenario);
Field fieldStepResults = objectScenario.getClass().getDeclaredField("stepResults");
fieldStepResults.setAccessible(true);
ArrayList<Result> results = (ArrayList<Result>) fieldStepResults.get(objectScenario);
for (Result result : results) {
if (result.getError() != null) {
LOGGER.error(String.format("Error Scenario: %s", scenario.getId()), result.getError());
}
}
} catch (Exception e) {
LOGGER.error("Error while logging error", e);
}
}
For cucumber-js https://www.npmjs.com/package/cucumber/v/6.0.3
import { After } from 'cucumber'
After(async function(scenario: any) {
const exception = scenario.result.exception
if (exception) {
this.logger.log({ level: 'error', message: '-----------StackTrace-----------' })
this.logger.log({ level: 'error', message: exception.stack })
this.logger.log({ level: 'error', message: '-----------End-StackTrace-----------' })
}
})
After a lot of experimentation I now removed the Before/After-Annotations and rely on Cucumber-Events instead. They contain the TestCase (which is what the Scenario-class wraps) and a Result where you can call getError(); to get the Throwable.
Here is a simple example to get it working
import io.cucumber.plugin.EventListener;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.Result;
import io.cucumber.plugin.event.Status;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestCaseFinished;
import io.cucumber.plugin.event.TestCaseStarted;
import org.openqa.selenium.WebDriver;
public class TestCaseListener implements EventListener {
#Override
public void setEventPublisher(final EventPublisher publisher) {
publisher.registerHandlerFor(TestCaseStarted.class, this::onTestCaseStarted);
publisher.registerHandlerFor(TestCaseFinished.class, this::onTestCaseFinished);
}
public void onTestCaseStarted(TestCaseStarted event) {
TestCase testCase = event.getTestCase();
System.out.println("Starting " + testCase.getName());
// Other stuff you did in your #Before-Method.
// ...
}
private void onTestCaseFinished(final TestCaseFinished event) {
TestCase testCase = event.getTestCase();
System.out.println("Finished " + testCase.getName());
Result result = event.getResult();
if (result.getStatus() == Status.FAILED) {
final Throwable error = result.getError();
error.printStackTrace();
}
// Other stuff you did in your #After-Method.
// ...
}
}
All that's left to do is to register this class as a Cucumber-Plugin.
I did this by modifying my #CucumberOptions-annotation:
#CucumberOptions(plugin = {"com.example.TestCaseListener"})
I find this much cleaner than all of this reflection-madness, however it requires a lot more code-changes.
Edit
I don't know why, but this caused a lot of tests to randomly fail in a multithreaded environment.
I tried to figure it out, but now also use the ugly reflections mentioned in this thread:
public class SeleniumUtils {
private static final Logger log = LoggerFactory.getLogger(SeleniumUtils.class);
private static final Field field = FieldUtils.getField(Scenario.class, "delegate", true);
private static Method getError;
public static Throwable getError(Scenario scenario) {
try {
final TestCaseState testCase = (TestCaseState) field.get(scenario);
if (getError == null) {
getError = MethodUtils.getMatchingMethod(testCase.getClass(), "getError");
getError.setAccessible(true);
}
return (Throwable) getError.invoke(testCase);
} catch (Exception e) {
log.warn("error receiving exception", e);
}
return null;
}
}
If you just want to massage the result being sent to the report then you can extend the CucumberJSONFormatter and override the result method like this:
public class CustomReporter extends CucumberJSONFormatter {
CustomReporter(Appendable out) {
super(out);
}
/**
* Truncate the error in the output to the testresult.json file.
* #param result the error result
*/
#Override
void result(Result result) {
String errorMessage = null;
if (result.error) {
errorMessage = "Error: " + truncateError(result.error);
}
Result myResult = new Result(result.status, result.duration, errorMessage);
// Log the truncated error to the JSON report
super.result(myResult);
}
}
Then set the plugin option to:
plugin = ["com.myimpl.CustomReporter:build/reports/json/testresult.json"]

Apache Camel Resequence Program Not Working

I am trying to run a simple re-sequence program using Apache Camel. This program uses the Java DSL to re-sequence incoming Java messages. When I run this program the messages are written to the folder but do not appear to be in any particular order based on the header value or the alphabetical order of the single word in the message body. The files Camel creates are out of order still as if the resequence DSL function did nothing.
How can I get this program to actually order the messages like the Arrays.sort() method would do? Also, how can I get this program to resequence and then aggregate the messages in the correct sort order to a single file?
Here is the program... I call the main Camel route via the other class that has the main method.
import org.apache.camel.builder.RouteBuilder;
public class SortThoseMessages extends RouteBuilder {
#Override
public void configure() throws Exception {
from("direct:pointA")
.resequence(header("grocery"))
.to("file:target/pointB");
}
}
The class below has main and produces the messages into the queue, pointA.
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.ProducerTemplate;
public class NewSequenceMain {
public static void main(String[] args) {
CamelContext c = new DefaultCamelContext();
try {
c.addRoutes(new SortThoseMessages());
ProducerTemplate template = c.createProducerTemplate();
c.start();
template.sendBodyAndHeader("direct:pointA", "apple", "grocery", 1);
template.sendBodyAndHeader("direct:pointA", "orange", "grocery", 3);
template.sendBodyAndHeader("direct:pointA", "bannanna", "grocery", 2);
Thread.sleep(5000);
c.stop();
} catch(Exception ex) {
System.err.println("Exception thrown -> " + ex);
System.err.println("Now printing stacktrace...");
ex.printStackTrace();
}
}
}
The messages maybe re-sequenced inside the Camel route but not when written to file. To see the re-sequence call the aggregator Java DSL to see the message bodies in the sequence order specified. The messages in the program posted are ordered according to the number of the header. The method call on the ProducerTemplate object reference sets the header as the integer in the last argument to the sendBodyAndHeader() method call.
To see the re-sequencing take effect in a single file as the destination of the Camel route please check out the example below:
import org.apache.camel.builder.RouteBuilder;
public class ReorganizingMessages extends RouteBuilder {
#Override
public void configure() throws Exception {
from("direct:pointA")
.resequence(header("grocery"))
.to("log://org.apache.camel.howto?showAll=true&multiline=true")
.aggregate().constant(true).completionTimeout(100L).
aggregationStrategy(new StringAggregator())
.to("file:target/pointB");
}
}
The code above uses a custom aggregator Java bean that can be seen below.
import org.apache.camel.Exchange;
import org.apache.camel.processor.aggregate.AggregationStrategy;
public class StringAggregator implements AggregationStrategy {
#Override
public Exchange aggregate(Exchange old, Exchange new1) {
if (old == null) {
return new1;
}
String oldBody = old.getIn().getBody(String.class);
String newBody = new1.getIn().getBody(String.class);
old.getIn().setBody(oldBody + " " + newBody);
return old;
}
}

Cannot get signal in DBus with Java

I've been playing around with Dbus 2.7 and java for a while and I almost got what I wanted, but eventually got stuck anew.
What I need is a java program that listens to the SYSTEM bus, particularly the org.freedesktop.UDisks interface signals, in order to detect when some devices (most often block devices) are added or removed from the machine( for me that means "plugged the device in and out"). Note that I only need to detect these signals, not the information that travels along with them.
The dbus signals in which I'm concretely interested are DeviceAdded and DeviceRemoved:
[dbus-monitor output]
signal sender=:1.45 -> dest=(null destination) serial=186 path=/org/freedesktop/UDisks; interface=org.freedesktop.UDisks; member=DeviceAdded
object path "/org/freedesktop/UDisks/devices/sdb"
signal sender=:1.45 -> dest=(null destination) serial=230 path=/org/freedesktop/UDisks; interface=org.freedesktop.UDisks; member=DeviceRemoved
object path "/org/freedesktop/UDisks/devices/sdb"
To accomplish this task in Java I created the following classes [1] and [2]:
[1] org.freedesktop.UDisks.java
/** Created with 'createinterface' utility on org.freedesktop.UDisks system bus interface**/
package org.freedesktop;
import java.util.List;
import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusSignal;
import org.freedesktop.dbus.UInt32;
import org.freedesktop.dbus.UInt64;
import org.freedesktop.dbus.exceptions.DBusException;
public interface UDisks extends DBusInterface
{
/** ... **/
public static class DeviceRemoved extends DBusSignal
{
public final DBusInterface a;
public DeviceRemoved(String path, DBusInterface a) throws DBusException
{
super(path, a);
this.a = a;
}
}
public static class DeviceAdded extends DBusSignal
{
public final DBusInterface a;
public DeviceAdded(String path, DBusInterface a) throws DBusException
{
super(path, a);
this.a = a;
}
}
public void Uninhibit(String cookie);
public String Inhibit();
public DBusInterface LinuxMdCreate(List<DBusInterface> components, String level, UInt64 stripe_size, String name, List<String> options);
...
/** ... Remaining code removed for the sake of post length**/
}
[2] org.freedesktop.ListenHWDBusSignal.java
package org.freedesktop;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.DBusSigHandler;
import org.freedesktop.dbus.exceptions.DBusException;
public class ListenHWDBusSignal {
public static void main(String[] args) throws DBusException, ParseException {
System.out.println("Creating Connection");
DBusConnection conn=DBusConnection.getConnection(DBusConnection.SYSTEM);
conn.addSigHandler(UDisks.DeviceAdded.class,new DBusSigHandler<UDisks.DeviceAdded>() {
#Override
public void handle(
UDisks.DeviceAdded added) {
System.out.println("Device added!");
}
});
conn.addSigHandler(UDisks.DeviceRemoved.class,new DBusSigHandler<UDisks.DeviceRemoved>() {
#Override
public void handle(
UDisks.DeviceRemoved removed) {
System.out.println("Device removed!");
}
});
System.out.println("Waiting for signals...");
}
}
All right, I execute [2], all goes fine and the program waits for signals:
Creating Connection
Waiting for signals...
When I manually add a usb device, I get this line:
Device added!
but when I remove manually the usb, and I mean not safely-removing it, just plugging it out, I get nothing.
I also checked the bus signals with dbus-monitor and see the DeviceRemoved signal is actually sent by org.freedesktop.UDisks to the system bus, so I think there's a problem on the java side that I am not seeing. Why I do not get the line "Device removed!"? Is there something wrong in the code?
Any help would be really appreciated, thanks!

Categories