1313
1414package io .dapr .client ;
1515
16+ import com .fasterxml .jackson .databind .ObjectMapper ;
1617import com .google .common .base .Strings ;
1718import com .google .protobuf .Any ;
1819import com .google .protobuf .ByteString ;
5960import io .dapr .client .domain .UnsubscribeConfigurationRequest ;
6061import io .dapr .client .domain .UnsubscribeConfigurationResponse ;
6162import io .dapr .client .resiliency .ResiliencyOptions ;
63+ import io .dapr .config .Properties ;
6264import io .dapr .exceptions .DaprException ;
6365import io .dapr .internal .exceptions .DaprHttpException ;
6466import io .dapr .internal .grpc .DaprClientGrpcInterceptors ;
9294import reactor .util .retry .Retry ;
9395
9496import java .io .IOException ;
97+ import java .nio .charset .Charset ;
9598import java .time .Duration ;
9699import java .util .ArrayList ;
97100import java .util .Arrays ;
@@ -140,6 +143,10 @@ public class DaprClientImpl extends AbstractDaprClient {
140143 private final DaprHttp httpClient ;
141144
142145 private final DaprClientGrpcInterceptors grpcInterceptors ;
146+
147+ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
148+
149+ private static final Charset CHARSET = Properties .STRING_CHARSET .get ();
143150
144151 /**
145152 * Default access level constructor, in order to create an instance of this class use io.dapr.client.DaprClientBuilder
@@ -1313,10 +1320,15 @@ public <T> Mono<Void> scheduleJobAlpha1(Job<T> job) {
13131320
13141321 DaprProtos .Job .Builder jobBuilder = DaprProtos .Job .newBuilder ()
13151322 .setName (name );
1316- if (data instanceof Message ) {
1317- jobBuilder .setData (Any .pack ((Message )job .getData ()));
1323+ if (data instanceof String ) {
1324+ jobBuilder .setData (Any .newBuilder ().setValue (ByteString .copyFrom ((String ) data , CHARSET )));
1325+ } else if (data instanceof byte []) {
1326+ String base64 = OBJECT_MAPPER .writeValueAsString (data );
1327+ jobBuilder .setData (Any .newBuilder ().setValue (ByteString .copyFrom (base64 , CHARSET )));
13181328 } else {
1319- jobBuilder .setData (Any .newBuilder ().setValue (ByteString .copyFrom (this .objectSerializer .serialize (data ))));
1329+ return Mono .error (() -> {
1330+ throw new IllegalArgumentException ("Job data value must be String or byte[]" );
1331+ });
13201332 }
13211333 if (job .getSchedule () != null && !job .getSchedule ().trim ().isEmpty ()) {
13221334 jobBuilder .setSchedule (job .getSchedule ());
@@ -1342,6 +1354,46 @@ public <T> Mono<Void> scheduleJobAlpha1(Job<T> job) {
13421354 return DaprException .wrapMono (ex );
13431355 }
13441356 }
1357+
1358+ @ SuppressWarnings ("unchecked" )
1359+ @ Override
1360+ public <T > Mono <Job <T >> getJobAlpha1 (String name , Class <T > clazz ) {
1361+ try {
1362+ if (name == null || name .trim ().isEmpty ()) {
1363+ throw new IllegalArgumentException ("Job name cannot be null or empty" );
1364+ }
1365+ return this .<DaprProtos .GetJobResponse >createMono (
1366+ it -> intercept (null , asyncStub ).getJobAlpha1 (DaprProtos .GetJobRequest .newBuilder ().setName (name ).build (), it ))
1367+ .map (it -> {
1368+ DaprProtos .Job _job = it .getJob ();
1369+ T data = null ;
1370+ if (clazz .isInstance (String .class )) {
1371+ data = (T )_job .getData ().toByteString ().toString (CHARSET );
1372+ } else if (clazz .isInstance (byte [].class )) {
1373+ data = (T ) _job .getData ().toByteArray ();
1374+ } else {
1375+ throw new IllegalArgumentException ("Job data type must be String or byte[]" );
1376+ }
1377+ return new Job <>(_job .getName (), _job .getSchedule (), _job .getRepeats (), _job .getDueTime (), _job .getTtl (), data );
1378+ });
1379+ } catch (Exception ex ) {
1380+ return DaprException .wrapMono (ex );
1381+ }
1382+ }
1383+
1384+ @ Override
1385+ public Mono <Void > deleteJobAlpha1 (String name ) {
1386+ try {
1387+ if (name == null || name .trim ().isEmpty ()) {
1388+ throw new IllegalArgumentException ("Job name cannot be null or empty" );
1389+ }
1390+ return this .<DaprProtos .DeleteJobResponse >createMono (
1391+ it -> intercept (null , asyncStub ).deleteJobAlpha1 (DaprProtos .DeleteJobRequest .newBuilder ().setName (name ).build (), it ))
1392+ .then ();
1393+ } catch (Exception ex ) {
1394+ return DaprException .wrapMono (ex );
1395+ }
1396+ }
13451397
13461398 /**
13471399 * Build a new Configuration Item from provided parameter.
0 commit comments