This sample code is a smoke test to ensure that Azure Preview for Java work while loaded into the same process by performing 2 or more actions with them.
Libraries tested:
- keyvault-secrets
- identity
- storage-blob
- event-hubs
- cosmos
For this sample, it is necessary to create/have the following resources in the Azure Portal:
- App registration: Register a new app or use an existing one.
- Under Certificates & secrets create a new client secret and store the value in a safe place.
- Key Vaults: Create a new Key Vault resource or use an existing one.
- Under Access policies, add the app registered in the previous step.
- Storage accounts: Create a container in a new or existing storage account. The container in this sample is named "mycontainer", if you want to use other name you can change the value in
BlobStorage.ts
file:const containerName = "mycontainer";
- Event Hubs: Create an event hub inside a new or existing Event Hubs Namespace. The container in this sample is named "myeventhub", if you want to use other name you can change the value in
EventHubsTest.ts
file:let eventHubName = "myeventhub";
- Azure Cosmos DB: Create a new account or use an existing one.
The following environment variables are needed:
-
From App Registration, in the Overview section:
- AZURE_TENANT_ID: The directory tenant ID.
- AZURE_CLIENT_ID: The application ID.
- AZURE_CLIENT_SECRET: The client secret stored previously when creating the client secret.
-
From Key Vault, in the Overview section:
- AZURE_PROJECT_URL: The DNS Name
-
From Event Hubs, in Shared access policies section:
- AZURE_EVENT_HUBS_CONNECTION_STRING: Connection string from a policy
-
From Storage Account, in the Access Keys section:
- AZURE_STORAGE_CONNECTION_STRING : A connection strings.
-
From Azure Cosmos DB, in the Keys section, select the Read-Write Keys tab:
- AZURE_COSMOS_ENDPOINT: URI.
- AZURE_COSMOS_KEY: Primary or secondary key.
# Bash code to create the environment variables
export AZURE_CLIENT_ID=""
export AZURE_CLIENT_SECRET=""
export AZURE_TENANT_ID=""
export AZURE_EVENT_HUBS_CONNECTION_STRING=""
export AZURE_AZURE_PROJECT_URL=""
export AZURE_STORAGE_CONNECTION_STRING=""
export AZURE_COSMOS_ENDPOINT=""
export AZURE_COSMOS_KEY=""
The Azure clients use a ClientLogger. Create an environment variable AZURE_LOG_LEVEL
and set it to the desire level:
- Verbose = 1
- Informational = 2
- Warnings = 3
- Errors = 4
- Disabled = 5
Java version 11.0.4 was used to run this sample.
Install Maven dependencies:
Run App.main()
:
All the classes in this sample not depend on each other.
It is possible to run them individually:
package com.azure.smoketest;
import java.io.IOException;
public class App {
public static void main(String[] args) throws IllegalArgumentException, IOException {
StorageBlob.main(null);
}
}
The classes can be used as base code and be changed to satisfied specific needs. For example, the method EventHubs().sendAndReceiveEvents()
can be change to only send events from an array given from a parameter:
private void sendEvents(String partitionId, Flux<EventData> events) {
EventHubProducer producer = client.createProducer(new EventHubProducerOptions().partitionId(partitionId));
producer.send(events).subscribe(
(ignored) -> logger.info("sent"),
error -> logger.error("Error received:" + error),
() -> {
//Closing the producer once is done with sending the events
try {
producer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
);
}
Note: The methods in the classes are not necessary independent on each other, and the order matters. For example, in order to run StorageBlob.deleteBlob();
, the method StorageBlob.uploadBLob();
must be run before, since in the other way it will fail because there is not going to be a blob to delete.
Be sure to set the environment variables and credentials required before running the sample.
Be sure to include the SLF4J dependency in the pom.xml
file.
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.28</version>
</dependency>
Check the Azure SDK for Java Repository for more samples.
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.