Skip to content

Commit 685bb37

Browse files
authored
Merge pull request #31 from ehsavoie/simple_example_rest
fix: Adding simple example for HTTP+JSON/REST
2 parents c234de4 + 073fd82 commit 685bb37

6 files changed

Lines changed: 113 additions & 28 deletions

File tree

.github/workflows/build-and-test.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,31 @@ jobs:
106106
echo "Actual: $CLIENT_OUTPUT"
107107
exit 1
108108
fi
109+
- name: Test HTTP+JSON/REST simple example
110+
run: |
111+
# Build the server with rest
112+
mvn -B clean package -pl examples/simple/server -Prest -Dversion.sdk=${SDK_VERSION}
113+
114+
# Start server and wait
115+
mvn -B wildfly:start -pl examples/simple/server -Dstartup-timeout=120
116+
117+
# Run the client and capture output
118+
CLIENT_OUTPUT=$(mvn -B exec:java -pl examples/simple/client -Prun-rest -Duser.name="CI Run" -q 2>&1)
119+
120+
# Stop server
121+
mvn -B wildfly:shutdown -pl examples/simple/server
122+
123+
# Validate output
124+
EXPECTED="Agent responds: Hello CI Run"
125+
if [[ "$CLIENT_OUTPUT" == *"$EXPECTED"* ]]; then
126+
echo "✅ HTTP+JSON/REST example test passed"
127+
echo "Output: $CLIENT_OUTPUT"
128+
else
129+
echo "❌ HTTP+JSON/REST example test failed"
130+
echo "Expected: $EXPECTED"
131+
echo "Actual: $CLIENT_OUTPUT"
132+
exit 1
133+
fi
109134
- name: Upload WildFly logs on test failure
110135
if: failure()
111136
uses: actions/upload-artifact@v4

examples/simple/README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,26 @@ mvn clean install -Pgrpc -f server/pom.xml
8888
mvn exec:java -f client/pom.xml -Prun-grpc -Duser.name=Kabir
8989
````
9090

91-
You should see the output: `Agent responds: Hello kabir`
91+
You should see the output: `Agent responds: Hello Kabir`
92+
93+
### HTTP+JSON/REST
94+
95+
1. Build the server
96+
````shell
97+
mvn clean install -Prest -f server/pom.xml
98+
````
99+
100+
2. Start the server
101+
102+
````shell
103+
./server/target/wildfly/bin/standalone.sh --stability=preview
104+
````
105+
106+
3. Run the client
107+
108+
````shell
109+
mvn exec:java -f client/pom.xml -Prun-rest -Duser.name=Kabir
110+
````
111+
112+
You should see the output: `Agent responds: Hello Kabir`
92113

examples/simple/client/pom.xml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
<artifactId>a2a-java-sdk-jakarta-example-simple-client</artifactId>
1515

16-
<packaging>war</packaging>
16+
<packaging>jar</packaging>
1717

18-
<name>WildFly Extras - Java A2A SDK for Jakarta - Example - Simple</name>
18+
<name>WildFly Extras - Java A2A SDK for Jakarta - Example - Simple Client</name>
1919
<description>Java SDK for the Agent2Agent Protocol (A2A) - SDK - Jakarta - TCK - WildFly Jar</description>
2020

2121
<properties>
@@ -34,7 +34,11 @@
3434
</dependency>
3535
<dependency>
3636
<groupId>io.github.a2asdk</groupId>
37-
<artifactId>a2a-java-sdk-client-transport-grpc</artifactId>
37+
<artifactId>a2a-java-sdk-client-transport-jsonrpc</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>io.github.a2asdk</groupId>
41+
<artifactId>a2a-java-sdk-client-transport-rest</artifactId>
3842
</dependency>
3943

4044
<!-- Dependencies needed for gRPC -->
@@ -57,7 +61,6 @@
5761
<plugin>
5862
<groupId>org.codehaus.mojo</groupId>
5963
<artifactId>exec-maven-plugin</artifactId>
60-
<version>3.1.0</version>
6164
<configuration>
6265
<mainClass>org.wildfly.extras.a2a.examples.simple.client.SimpleExampleClient</mainClass>
6366
</configuration>
@@ -100,5 +103,22 @@
100103
</plugins>
101104
</build>
102105
</profile>
106+
<profile>
107+
<id>run-rest</id>
108+
<build>
109+
<plugins>
110+
<plugin>
111+
<groupId>org.codehaus.mojo</groupId>
112+
<artifactId>exec-maven-plugin</artifactId>
113+
<configuration>
114+
<arguments>
115+
<argument>HTTP+JSON</argument>
116+
<argument>${user.name}</argument>
117+
</arguments>
118+
</configuration>
119+
</plugin>
120+
</plugins>
121+
</build>
122+
</profile>
103123
</profiles>
104124
</project>

examples/simple/client/src/main/java/org/wildfly/extras/a2a/examples/simple/client/SimpleExampleClient.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package org.wildfly.extras.a2a.examples.simple.client;
22

3-
import java.io.Closeable;
43
import java.util.ArrayList;
5-
import java.util.Arrays;
64
import java.util.Collections;
75
import java.util.List;
86
import java.util.concurrent.CompletableFuture;
9-
import java.util.concurrent.CountDownLatch;
107
import java.util.concurrent.TimeUnit;
11-
import java.util.concurrent.atomic.AtomicReference;
128
import java.util.function.BiConsumer;
139

1410
import io.a2a.A2A;
@@ -22,6 +18,8 @@
2218
import io.a2a.client.transport.grpc.GrpcTransportConfigBuilder;
2319
import io.a2a.client.transport.jsonrpc.JSONRPCTransport;
2420
import io.a2a.client.transport.jsonrpc.JSONRPCTransportConfigBuilder;
21+
import io.a2a.client.transport.rest.RestTransport;
22+
import io.a2a.client.transport.rest.RestTransportConfigBuilder;
2523
import io.a2a.spec.A2AClientError;
2624
import io.a2a.spec.A2AClientException;
2725
import io.a2a.spec.AgentCard;
@@ -41,16 +39,17 @@ public SimpleExampleClient(String protocol) throws A2AClientError, A2AClientExce
4139

4240
ClientConfig config = new ClientConfig.Builder()
4341
.setAcceptedOutputModes(List.of("text"))
42+
.setUseClientPreference(true)
4443
.build();
4544

4645
ClientBuilder clientBuilder = Client.builder(agentCard)
4746
.clientConfig(config);
4847

49-
if (protocol.equals(TransportProtocol.JSONRPC.name())) {
50-
clientBuilder.withTransport(JSONRPCTransport.class, new JSONRPCTransportConfigBuilder());
51-
} else {
52-
// 'target' comes from the AgentCard, and is populated by a2a-java
53-
clientBuilder.withTransport(
48+
TransportProtocol prot = TransportProtocol.fromString(protocol);
49+
switch (prot) {
50+
case JSONRPC -> clientBuilder.withTransport(JSONRPCTransport.class, new JSONRPCTransportConfigBuilder());
51+
case HTTP_JSON -> clientBuilder.withTransport(RestTransport.class, new RestTransportConfigBuilder());
52+
case GRPC -> clientBuilder.withTransport(
5453
GrpcTransport.class,
5554
new GrpcTransportConfigBuilder().channelFactory(
5655
target -> {

examples/simple/server/pom.xml

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
<packaging>war</packaging>
1717

18-
<name>WildFly Extras - Java A2A SDK for Jakarta - Example - Simple</name>
19-
<description>Java SDK for the Agent2Agent Protocol (A2A) - SDK - Jakarta - TCK - WildFly Jar</description>
18+
<name>WildFly Extras - Java A2A SDK for Jakarta - Example - Simple Server</name>
19+
<description>Java SDK for the Agent2Agent Protocol (A2A) - SDK - Jakarta - Example - Simple</description>
2020

2121
<properties>
2222
<!--
@@ -40,8 +40,8 @@
4040
</dependency>
4141

4242
<!--
43-
The core a2a-java server dependency. Default scope since it, and
44-
a lot of its dependencies should be included in the war. -->
43+
The core a2a-java server dependency. Default scope since it, and
44+
a lot of its dependencies should be included in the war. -->
4545
<dependency>
4646
<groupId>io.github.a2asdk</groupId>
4747
<artifactId>a2a-java-sdk-server-common</artifactId>
@@ -79,7 +79,7 @@
7979
<groupId>org.wildfly.plugins</groupId>
8080
<artifactId>wildfly-maven-plugin</artifactId>
8181
<configuration>
82-
<feature-packs>
82+
<feature-packs>
8383
<feature-pack>
8484
<groupId>org.wildfly</groupId>
8585
<artifactId>wildfly-galleon-pack</artifactId>
@@ -157,10 +157,10 @@
157157
</dependency>
158158

159159
<!--
160-
gRPC needs JSONRPC to get the agent card.
161-
This is likely to change in the future. So for now include the
162-
JSONRCP dependencies here.
163-
-->
160+
gRPC needs JSONRPC to get the agent card.
161+
This is likely to change in the future. So for now include the
162+
JSONRCP dependencies here.
163+
-->
164164
<dependency>
165165
<groupId>${project.groupId}</groupId>
166166
<artifactId>a2a-java-sdk-jakarta-jsonrpc</artifactId>
@@ -174,7 +174,7 @@
174174
<groupId>org.wildfly.plugins</groupId>
175175
<artifactId>wildfly-maven-plugin</artifactId>
176176
<configuration>
177-
<feature-packs>
177+
<feature-packs>
178178
<!--
179179
Still include the main WildFly feature pack, so we are not stuck
180180
to the version used by the gRPC feature pack
@@ -184,7 +184,7 @@
184184
<artifactId>wildfly-galleon-pack</artifactId>
185185
<version>${version.wildfly}</version>
186186
</feature-pack>
187-
<!-- Add the gRPC feature pack -->
187+
<!-- Add the gRPC feature pack -->
188188
<feature-pack>
189189
<groupId>org.wildfly.extras.grpc</groupId>
190190
<artifactId>wildfly-grpc-feature-pack</artifactId>
@@ -231,9 +231,16 @@
231231
<artifactId>a2a-java-sdk-jakarta-rest</artifactId>
232232
<version>${project.version}</version>
233233
</dependency>
234+
235+
<!--
236+
gRPC needs JSONRPC to get the agent card.
237+
This is likely to change in the future. So for now include the
238+
JSONRCP dependencies here.
239+
-->
234240
<dependency>
235-
<groupId>com.google.protobuf</groupId>
236-
<artifactId>protobuf-java-util</artifactId>
241+
<groupId>${project.groupId}</groupId>
242+
<artifactId>a2a-java-sdk-jakarta-jsonrpc</artifactId>
243+
<version>${project.version}</version>
237244
</dependency>
238245
</dependencies>
239246
</profile>

examples/simple/server/src/main/java/org/wildfly/extras/a2a/examples/simple/SimpleExampleAgentCardProducer.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ public AgentCard createAgentCard() {
2727
interfaces.add(
2828
new AgentInterface(
2929
TransportProtocol.JSONRPC.asString(), jsonRpcUrl));
30-
30+
if(isRest()) {
31+
interfaces.add(
32+
new AgentInterface(
33+
TransportProtocol.HTTP_JSON.asString(), jsonRpcUrl));
34+
}
3135
if (isGrpcEnabled()) {
3236
interfaces.add(
3337
new AgentInterface(
@@ -63,4 +67,13 @@ private boolean isGrpcEnabled() {
6367
return false;
6468
}
6569
}
70+
71+
private boolean isRest() {
72+
try {
73+
Class.forName("org.wildfly.extras.a2a.server.apps.rest.WildflyRestTransportMetadata");
74+
return true;
75+
} catch (Throwable t) {
76+
return false;
77+
}
78+
}
6679
}

0 commit comments

Comments
 (0)