generated from pagopa/template-java-spring-microservice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
377f02d
commit a3f386c
Showing
8 changed files
with
266 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 42 additions & 30 deletions
72
src/main/java/it/gov/pagopa/node/cfg_sync/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,52 @@ | ||
package it.gov.pagopa.node.cfg_sync; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; | ||
|
||
@SpringBootApplication | ||
@EnableAutoConfiguration(exclude = { | ||
DataSourceAutoConfiguration.class, | ||
DataSourceTransactionManagerAutoConfiguration.class, | ||
HibernateJpaAutoConfiguration.class}) | ||
@Slf4j | ||
public class Application implements CommandLineRunner { | ||
|
||
//package it.gov.pagopa.node.cfg_sync; | ||
// | ||
//import com.azure.messaging.eventhubs.EventData; | ||
//import com.azure.messaging.eventhubs.EventHubProducerClient; | ||
//import com.azure.messaging.eventhubs.EventProcessorClient; | ||
//import lombok.extern.slf4j.Slf4j; | ||
//import org.springframework.boot.CommandLineRunner; | ||
//import org.springframework.boot.SpringApplication; | ||
//import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
//import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
//import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
//import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; | ||
//import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; | ||
// | ||
//import java.util.Collections; | ||
//import java.util.concurrent.TimeUnit; | ||
// | ||
//@SpringBootApplication | ||
//@EnableAutoConfiguration(exclude = { | ||
// DataSourceAutoConfiguration.class, | ||
// DataSourceTransactionManagerAutoConfiguration.class, | ||
// HibernateJpaAutoConfiguration.class}) | ||
//@Slf4j | ||
//public class Application implements CommandLineRunner { | ||
// | ||
//// private final EventHubProducerClient eventHubProducerClient; | ||
// private final EventProcessorClient eventProcessorClient; | ||
// | ||
// public Application(EventProcessorClient eventProcessorClient) { | ||
// public Application(//EventHubProducerClient eventHubProducerClient, | ||
// EventProcessorClient eventProcessorClient) { | ||
//// this.eventHubProducerClient = eventHubProducerClient; | ||
// this.eventProcessorClient = eventProcessorClient; | ||
// } | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
// | ||
// public static void main(String[] args) { | ||
// SpringApplication.run(Application.class, args); | ||
// } | ||
// | ||
// @Override | ||
// public void run(String... args) throws Exception { | ||
// eventProcessorClient.start(); | ||
// // Wait for the processor client to be ready | ||
// TimeUnit.SECONDS.sleep(10); | ||
// | ||
//// eventHubProducerClient.send(Collections.singletonList(new EventData("Hello World"))); | ||
//// log.info("Successfully sent a message to Event Hubs."); | ||
//// eventHubProducerClient.close(); | ||
// log.info("Stopping and closing the processor"); | ||
// eventProcessorClient.stop(); | ||
} | ||
|
||
} | ||
// } | ||
// | ||
//} |
42 changes: 42 additions & 0 deletions
42
src/main/java/it/gov/pagopa/node/cfg_sync/EventHubClientApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package it.gov.pagopa.node.cfg_sync; | ||
|
||
import com.azure.messaging.eventhubs.EventProcessorClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@SpringBootApplication | ||
public class EventHubClientApplication implements CommandLineRunner { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(EventHubClientApplication.class); | ||
// private final EventHubProducerClient eventHubProducerClient; | ||
private final EventProcessorClient eventProcessorClient; | ||
|
||
public EventHubClientApplication(//EventHubProducerClient eventHubProducerClient, | ||
EventProcessorClient eventProcessorClient) { | ||
// this.eventHubProducerClient = eventHubProducerClient; | ||
this.eventProcessorClient = eventProcessorClient; | ||
} | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(EventHubClientApplication.class, args); | ||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
eventProcessorClient.start(); | ||
// Wait for the processor client to be ready | ||
TimeUnit.SECONDS.sleep(10); | ||
|
||
// eventHubProducerClient.send(Collections.singletonList(new EventData("Hello World"))); | ||
// LOGGER.info("Successfully sent a message to Event Hubs."); | ||
// eventHubProducerClient.close(); | ||
LOGGER.info("Stopping and closing the processor"); | ||
eventProcessorClient.stop(); | ||
} | ||
|
||
} |
162 changes: 83 additions & 79 deletions
162
src/main/java/it/gov/pagopa/node/cfg_sync/config/EventHubClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,86 @@ | ||
//package it.gov.pagopa.node.cfg_sync.config; | ||
// | ||
//import com.azure.identity.DefaultAzureCredentialBuilder; | ||
//import com.azure.messaging.eventhubs.EventHubClientBuilder; | ||
//import com.azure.messaging.eventhubs.EventHubProducerClient; | ||
//import com.azure.messaging.eventhubs.EventProcessorClient; | ||
//import com.azure.messaging.eventhubs.EventProcessorClientBuilder; | ||
//import com.azure.messaging.eventhubs.checkpointstore.blob.BlobCheckpointStore; | ||
//import com.azure.messaging.eventhubs.models.ErrorContext; | ||
//import com.azure.messaging.eventhubs.models.EventContext; | ||
//import com.azure.storage.blob.BlobContainerAsyncClient; | ||
//import com.azure.storage.blob.BlobContainerClientBuilder; | ||
//import org.slf4j.Logger; | ||
//import org.slf4j.LoggerFactory; | ||
//import org.springframework.beans.factory.annotation.Value; | ||
//import org.springframework.context.annotation.Bean; | ||
//import org.springframework.context.annotation.Configuration; | ||
// | ||
//@Configuration | ||
//public class EventHubClientConfiguration { | ||
// | ||
// private static final Logger LOGGER = LoggerFactory.getLogger(EventHubClientConfiguration.class); | ||
// private static final String CONSUMER_GROUP = "$Default"; | ||
// private static final String STORAGE_ACCOUNT_ENDPOINT = "https://pagopadapiconfigfesa.blob.core.windows.net/"; | ||
// private static final String STORAGE_CONTAINER_NAME = "cfg-sync"; | ||
// | ||
// @Value("${nodo-dei-pagamenti-cache-rx-connection-string}") | ||
// private String ndpConnectionString; | ||
// | ||
// @Value("${nodo-dei-pagamenti-cache-rx-name}") | ||
// private String ndpEventHubName; | ||
// | ||
// @Bean | ||
// EventHubClientBuilder eventHubClientBuilder() { | ||
// return new EventHubClientBuilder().credential(ndpConnectionString, ndpEventHubName, | ||
// new DefaultAzureCredentialBuilder() | ||
// .build()); | ||
// } | ||
// | ||
// @Bean | ||
// BlobContainerClientBuilder blobContainerClientBuilder() { | ||
// return new BlobContainerClientBuilder().credential(new DefaultAzureCredentialBuilder() | ||
// .build()) | ||
// .endpoint(STORAGE_ACCOUNT_ENDPOINT) | ||
// .containerName(STORAGE_CONTAINER_NAME); | ||
// } | ||
// | ||
// @Bean | ||
// BlobContainerAsyncClient blobContainerAsyncClient(BlobContainerClientBuilder blobContainerClientBuilder) { | ||
// return blobContainerClientBuilder.buildAsyncClient(); | ||
// } | ||
// | ||
package it.gov.pagopa.node.cfg_sync.config; | ||
|
||
import com.azure.identity.DefaultAzureCredentialBuilder; | ||
import com.azure.messaging.eventhubs.EventHubClientBuilder; | ||
import com.azure.messaging.eventhubs.EventHubProducerClient; | ||
import com.azure.messaging.eventhubs.EventProcessorClient; | ||
import com.azure.messaging.eventhubs.EventProcessorClientBuilder; | ||
import com.azure.messaging.eventhubs.checkpointstore.blob.BlobCheckpointStore; | ||
import com.azure.messaging.eventhubs.models.ErrorContext; | ||
import com.azure.messaging.eventhubs.models.EventContext; | ||
import com.azure.storage.blob.BlobContainerAsyncClient; | ||
import com.azure.storage.blob.BlobContainerClientBuilder; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@Slf4j | ||
public class EventHubClientConfiguration { | ||
|
||
private static final String CONSUMER_GROUP = "$Default"; | ||
private static final String STORAGE_ACCOUNT_ENDPOINT = ""; | ||
private static final String STORAGE_CONTAINER_NAME = "prova"; | ||
|
||
@Value("${nodo-dei-pagamenti-cache-rx-connection-string}") | ||
private String nodoCacheRxConnectionString; | ||
@Value("${nodo-dei-pagamenti-cache-rx-name}") | ||
private String nodoCacheRxName; | ||
|
||
@Bean | ||
EventHubClientBuilder eventHubClientBuilder() { | ||
return new EventHubClientBuilder().credential(nodoCacheRxConnectionString, nodoCacheRxName, | ||
new DefaultAzureCredentialBuilder() | ||
.build()); | ||
} | ||
|
||
@Bean | ||
BlobContainerClientBuilder blobContainerClientBuilder() { | ||
return new BlobContainerClientBuilder().credential(new DefaultAzureCredentialBuilder() | ||
.build()) | ||
.endpoint(STORAGE_ACCOUNT_ENDPOINT) | ||
.containerName(STORAGE_CONTAINER_NAME); | ||
} | ||
|
||
@Bean | ||
BlobContainerAsyncClient blobContainerAsyncClient(BlobContainerClientBuilder blobContainerClientBuilder) { | ||
return blobContainerClientBuilder.buildAsyncClient(); | ||
} | ||
|
||
@Bean | ||
EventProcessorClientBuilder eventProcessorClientBuilder(BlobContainerAsyncClient blobContainerAsyncClient) { | ||
return new EventProcessorClientBuilder().credential(nodoCacheRxConnectionString, nodoCacheRxName, | ||
new DefaultAzureCredentialBuilder() | ||
.build()) | ||
.consumerGroup(CONSUMER_GROUP) | ||
.checkpointStore(new BlobCheckpointStore(blobContainerAsyncClient)) | ||
.processEvent(EventHubClientConfiguration::processEvent) | ||
.processError(EventHubClientConfiguration::processError); | ||
} | ||
|
||
// @Bean | ||
// EventProcessorClientBuilder eventProcessorClientBuilder(BlobContainerAsyncClient blobContainerAsyncClient) { | ||
// return new EventProcessorClientBuilder().credential(ndpConnectionString, ndpEventHubName, | ||
// new DefaultAzureCredentialBuilder() | ||
// .build()) | ||
// .consumerGroup(CONSUMER_GROUP) | ||
// .checkpointStore(new BlobCheckpointStore(blobContainerAsyncClient)) | ||
// .processEvent(EventHubClientConfiguration::processEvent) | ||
// .processError(EventHubClientConfiguration::processError); | ||
// } | ||
// EventHubProducerClient eventHubProducerClient(EventHubClientBuilder eventHubClientBuilder) { | ||
// return eventHubClientBuilder.buildProducerClient(); | ||
// | ||
// @Bean | ||
// EventProcessorClient eventProcessorClient(EventProcessorClientBuilder eventProcessorClientBuilder) { | ||
// return eventProcessorClientBuilder.buildEventProcessorClient(); | ||
// } | ||
// | ||
// public static void processEvent(EventContext eventContext) { | ||
// LOGGER.info("Processing event from partition {} with sequence number {} with body: {}", | ||
// eventContext.getPartitionContext().getPartitionId(), eventContext.getEventData().getSequenceNumber(), | ||
// eventContext.getEventData().getBodyAsString()); | ||
// } | ||
// | ||
// public static void processError(ErrorContext errorContext) { | ||
// LOGGER.info("Error occurred in partition processor for partition {}, {}", | ||
// errorContext.getPartitionContext().getPartitionId(), | ||
// errorContext.getThrowable().getMessage(), | ||
// errorContext.getThrowable()); | ||
// } | ||
// | ||
//} | ||
|
||
@Bean | ||
EventProcessorClient eventProcessorClient(EventProcessorClientBuilder eventProcessorClientBuilder) { | ||
return eventProcessorClientBuilder.buildEventProcessorClient(); | ||
} | ||
|
||
public static void processEvent(EventContext eventContext) { | ||
log.info("Processing event from partition {} with sequence number {} with body: {}", | ||
eventContext.getPartitionContext().getPartitionId(), eventContext.getEventData().getSequenceNumber(), | ||
eventContext.getEventData().getBodyAsString()); | ||
} | ||
|
||
public static void processError(ErrorContext errorContext) { | ||
log.info("Error occurred in partition processor for partition {}, {}", | ||
errorContext.getPartitionContext().getPartitionId(), | ||
errorContext.getThrowable().getMessage(), | ||
errorContext.getThrowable()); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/it/gov/pagopa/node/cfg_sync/config/EventHubProcessorClientConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package it.gov.pagopa.node.cfg_sync.config; | ||
|
||
import com.azure.spring.cloud.service.eventhubs.consumer.EventHubsErrorHandler; | ||
import com.azure.spring.cloud.service.eventhubs.consumer.EventHubsRecordMessageListener; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@Slf4j | ||
public class EventHubProcessorClientConfiguration { | ||
|
||
@Bean | ||
EventHubsRecordMessageListener processEvent() { | ||
return eventContext->log.info("Processing event from partition {} with sequence number {} with body: {}", | ||
eventContext.getPartitionContext().getPartitionId(), eventContext.getEventData().getSequenceNumber(), | ||
eventContext.getEventData().getBodyAsString()); | ||
} | ||
|
||
@Bean | ||
EventHubsErrorHandler processError() { | ||
return errorContext->log.info("Error occurred in partition processor for partition {}, {}", | ||
errorContext.getPartitionContext().getPartitionId(), | ||
errorContext.getThrowable().getMessage(), | ||
errorContext.getThrowable()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.