88import com .solacecoe .connectors .spark .streaming .properties .SolaceSparkStreamingProperties ;
99import com .solacesystems .jcsmp .*;
1010import org .apache .spark .api .java .function .VoidFunction2 ;
11- import org .apache .spark .sql .Dataset ;
12- import org .apache .spark .sql .Row ;
13- import org .apache .spark .sql .SaveMode ;
14- import org .apache .spark .sql .SparkSession ;
11+ import org .apache .spark .sql .*;
1512import org .apache .spark .sql .streaming .DataStreamReader ;
1613import org .apache .spark .sql .streaming .StreamingQuery ;
1714import org .apache .spark .sql .streaming .StreamingQueryException ;
1815import org .junit .jupiter .api .*;
1916import org .testcontainers .junit .jupiter .Testcontainers ;
17+ import org .testcontainers .shaded .org .apache .commons .io .FileUtils ;
2018import org .testcontainers .solace .Service ;
2119import org .testcontainers .solace .SolaceContainer ;
2220
21+ import java .io .File ;
22+ import java .io .IOException ;
23+ import java .nio .file .Files ;
2324import java .nio .file .Path ;
2425import java .nio .file .Paths ;
2526import java .util .concurrent .ExecutorService ;
@@ -88,6 +89,8 @@ public void handleErrorEx(Object o, JCSMPException e, long l) {
8889 TextMessage textMessage = JCSMPFactory .onlyInstance ().createMessage (TextMessage .class );
8990 textMessage .setText ("Hello Spark!" );
9091 textMessage .setPriority (1 );
92+ textMessage .setCorrelationId ("test-correlation-id" );
93+ textMessage .setDMQEligible (true );
9194 Topic topic = JCSMPFactory .onlyInstance ().createTopic ("solace/spark/streaming" );
9295 messageProducer .send (textMessage , topic );
9396 }
@@ -99,6 +102,22 @@ public void handleErrorEx(Object o, JCSMPException e, long l) {
99102 }
100103 }
101104
105+ @ AfterEach
106+ public void afterEach () throws IOException {
107+ Path path = Paths .get ("src" , "test" , "resources" , "spark-checkpoint-1" );
108+ Path path1 = Paths .get ("src" , "test" , "resources" , "spark-checkpoint-2" );
109+ Path path2 = Paths .get ("src" , "test" , "resources" , "spark-checkpoint-3" );
110+ if (Files .exists (path )) {
111+ FileUtils .deleteDirectory (path .toAbsolutePath ().toFile ());
112+ }
113+ if (Files .exists (path1 )) {
114+ FileUtils .deleteDirectory (path1 .toAbsolutePath ().toFile ());
115+ }
116+ if (Files .exists (path2 )) {
117+ FileUtils .deleteDirectory (path2 .toAbsolutePath ().toFile ());
118+ }
119+ }
120+
102121 @ Test
103122 public void Should_ProcessData () throws TimeoutException , StreamingQueryException {
104123 Path path = Paths .get ("src" , "test" , "resources" , "spark-checkpoint-1" );
@@ -277,7 +296,7 @@ public void Should_CreateMultipleConsumersOnSameSession_And_ProcessData() throws
277296
278297 @ Test
279298 public void Should_CreateMultipleConsumersOnDifferentSessions_And_ProcessData () throws TimeoutException , StreamingQueryException {
280- Path path = Paths .get ("src" , "test" , "resources" , "spark-checkpoint-3 " );
299+ Path path = Paths .get ("src" , "test" , "resources" , "spark-checkpoint-2 " );
281300// SparkSession sparkSession = SparkSession.builder()
282301// .appName("data_source_test")
283302// .master("local[*]")
@@ -662,6 +681,86 @@ public void Should_Fail_IfBatchSizeLessThan1() {
662681 });
663682 }
664683
684+ @ Test
685+ public void Should_ProcessData_And_Publish_As_Stream_To_Solace () throws TimeoutException , StreamingQueryException {
686+ Path path = Paths .get ("src" , "test" , "resources" , "spark-checkpoint-1" );
687+ Path writePath = Paths .get ("src" , "test" , "resources" , "spark-checkpoint-3" );
688+ // SparkSession sparkSession = SparkSession.builder()
689+ // .appName("data_source_test")
690+ // .master("local[*]")
691+ // .getOrCreate();
692+ DataStreamReader reader = sparkSession .readStream ()
693+ .option (SolaceSparkStreamingProperties .HOST , solaceContainer .getOrigin (Service .SMF ))
694+ .option (SolaceSparkStreamingProperties .VPN , solaceContainer .getVpn ())
695+ .option (SolaceSparkStreamingProperties .USERNAME , solaceContainer .getUsername ())
696+ .option (SolaceSparkStreamingProperties .PASSWORD , solaceContainer .getPassword ())
697+ .option (SolaceSparkStreamingProperties .QUEUE , "Solace/Queue/0" )
698+ .option (SolaceSparkStreamingProperties .BATCH_SIZE , "10" )
699+ .option ("checkpointLocation" , path .toAbsolutePath ().toString ())
700+ .format ("solace" );
701+ final long [] count = {0 };
702+ final boolean [] runProcess = {true };
703+ final String [] messageId = {"" };
704+ Dataset <Row > dataset = reader .load ();
705+
706+ StreamingQuery streamingQuery = dataset .writeStream ().option (SolaceSparkStreamingProperties .HOST , solaceContainer .getOrigin (Service .SMF ))
707+ .option (SolaceSparkStreamingProperties .VPN , solaceContainer .getVpn ())
708+ .option (SolaceSparkStreamingProperties .USERNAME , solaceContainer .getUsername ())
709+ .option (SolaceSparkStreamingProperties .PASSWORD , solaceContainer .getPassword ())
710+ // .option(SolaceSparkStreamingProperties.BATCH_SIZE, dataset.count())
711+ .option (SolaceSparkStreamingProperties .MESSAGE_ID , "my-default-id" )
712+ .option ("checkpointLocation" , writePath .toAbsolutePath ().toString ())
713+ // .mode(SaveMode.Append)
714+ .format ("solace" ).start ();
715+
716+ SolaceSession session = new SolaceSession (solaceContainer .getOrigin (Service .SMF ), solaceContainer .getVpn (), solaceContainer .getUsername (), solaceContainer .getPassword ());
717+ Topic topic = JCSMPFactory .onlyInstance ().createTopic ("solace/spark/streaming" );
718+ XMLMessageConsumer messageConsumer = null ;
719+ try {
720+ messageConsumer = session .getSession ().getMessageConsumer (new XMLMessageListener () {
721+ @ Override
722+ public void onReceive (BytesXMLMessage bytesXMLMessage ) {
723+ count [0 ] = count [0 ] + 1 ;
724+ if (count [0 ] == 100 ) {
725+ messageId [0 ] = bytesXMLMessage .getApplicationMessageId ();
726+ }
727+ }
728+
729+ @ Override
730+ public void onException (JCSMPException e ) {
731+
732+ }
733+ });
734+ session .getSession ().addSubscription (topic );
735+ messageConsumer .start ();
736+ } catch (JCSMPException e ) {
737+ throw new RuntimeException (e );
738+ }
739+
740+ ExecutorService executorService = Executors .newFixedThreadPool (1 );
741+ executorService .execute (() -> {
742+ do {
743+ if (count [0 ] == 100L ) {
744+ runProcess [0 ] = false ;
745+ try {
746+ Assertions .assertEquals ("my-default-id" , messageId [0 ], "MessageId mismatch" );
747+ streamingQuery .stop ();
748+ // sparkSession.close();
749+ executorService .shutdown ();
750+ } catch (TimeoutException e ) {
751+ throw new RuntimeException (e );
752+ }
753+ }
754+ try {
755+ Thread .sleep (100 );
756+ } catch (InterruptedException e ) {
757+ throw new RuntimeException (e );
758+ }
759+ } while (runProcess [0 ]);
760+ });
761+ streamingQuery .awaitTermination ();
762+ }
763+
665764 @ Test
666765 public void Should_ProcessData_And_Publish_With_CustomId_To_Solace () throws TimeoutException , StreamingQueryException {
667766 Path path = Paths .get ("src" , "test" , "resources" , "spark-checkpoint-1" );
0 commit comments