Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Test the messaging functionality

In the spring-petclinic-microservices repository, the spring-petclinic-messaging-emulator microservice is already prepared to send messages to an Azure Service Bus namespace. You can add this microservice to your current Spring Petclinic project in the parent pom.xml file, deploy it as an extra microservice in your Azure Spring Apps service and use this microservice’s public endpoint to send messages to your Service Bus namespace. Test this functionality and inspect whether messages end up in the Service Bus namespace you just created by using the Service Bus Explorer for the visits-requests queue. You can use the following guidance to implement these changes:

Step by step guidance

  1. As a first step in the parent pom.xml file, you’ll have to uncomment the module for the spring-petclinic-messaging-emulator in the <modules> element at line 26.

     <module>spring-petclinic-messaging-emulator</module>
    
  2. Update the compiled version of the microservices available by running an additional build.

    cd ~/workspaces/java-microservices-asa-e-lab/src
    mvn clean package -DskipTests -rf :spring-petclinic-messaging-emulator
    
  3. Create a new application in your Spring Apps service for the messaging-emulator and assign a public endpoint to it.

    MESSAGING_EMULATOR=messaging-emulator
    az spring app create \
        --name $MESSAGING_EMULATOR \
        --assign-endpoint true
    

    Wait for the provisioning to complete. This might take about 3 minutes.

  4. Update the Application Configuration Service to also pick up the messaging-emulator config.

    az spring application-configuration-service git repo update \
        --resource-group $RESOURCE_GROUP \
        --name spring-petclinic-config \
        --service $SPRING_APPS_SERVICE \
        --label main \
        --patterns "api-gateway,customers-service,vets-service,visits-service,admin-server,messaging-emulator" \
        --uri $GIT_REPO \
        --password $GIT_PASSWORD \
        --username $GIT_USERNAME
    

    In case you are using a branch other than main in your config repo, you can change the branch name with the label parameter.

    Wait for the operation to complete. This might take about 2 minutes.

  5. Bind this new app to the Application Configuration Service and to the Service Registry.

    az spring application-configuration-service bind --app ${MESSAGING_EMULATOR}
       
    az spring service-registry bind --app ${MESSAGING_EMULATOR}
    
  6. Create a new user assigned managed identity for this new application and assign it to the messaging-emulator app:

    MESSAGING_EMULATOR_ID=$(az identity create -g $RESOURCE_GROUP -n messaging-svc-uid --query id -o tsv)
       
    az spring app identity assign \
        --resource-group $RESOURCE_GROUP \
        --name $MESSAGING_EMULATOR \
        --user-assigned $MESSAGING_EMULATOR_ID   
    
  7. Grant to the newly assigned identity the get and list permissions on your Key Vault secrets.

    MESSAGING_EMULATOR_UID=$(az identity show -g $RESOURCE_GROUP -n messaging-svc-uid --query principalId -o tsv)
       
    az keyvault set-policy \
        --name $KEYVAULT_NAME \
        --resource-group $RESOURCE_GROUP \
        --secret-permissions get list  \
        --object-id $MESSAGING_EMULATOR_UID
    
  8. Since the messaging-emulator will also save data in the database, you will also need to create a service connection for it:

    MESSAGING_EMULATOR_CID=$(az identity show -g $RESOURCE_GROUP -n messaging-svc-uid --query clientId -o tsv)
        
    az spring connection create mysql-flexible \
        --resource-group $RESOURCE_GROUP \
        --service $SPRING_APPS_SERVICE \
        --app $MESSAGING_EMULATOR \
        --target-resource-group $RESOURCE_GROUP \
        --server $MYSQL_SERVER_NAME \
        --database $DATABASE_NAME \
        --user-identity mysql-identity-id=$ADMIN_IDENTITY_RESOURCE_ID client-id=$MESSAGING_EMULATOR_CID subs-id=$SUBID
    
  9. You can now deploy the messaging-emulator application.

    MESSAGING_EMULATOR_JAR=spring-petclinic-messaging-emulator/target/spring-petclinic-messaging-emulator-$VERSION.jar
       
    az spring app deploy --name ${MESSAGING_EMULATOR} \
        --config-file-patterns ${MESSAGING_EMULATOR} \
        --artifact-path ${MESSAGING_EMULATOR_JAR}
    
  10. Switch to the web browser window displaying the Azure Portal, navigate to the resource group containing the resources you deployed in this lab, and, from there, navigate to the Azure Spring Apps Service.

  11. In the navigation menu, in the Settings section, select Apps, wait until the Provisioning state of the messaging-emulator app changes to Succeeded, and then select the messaging-emulator app entry.

    The provisioning might take about 3 minutes. Select Refresh in order to update the provisioning status.

  12. Once provisioning is complete. Select the apps URL and open this in a new browser window.

  13. On the newly opened browser page titled Message, enter 1 in the Pet text box and a random text in the Message text box, and then select Submit.

  14. In the Azure Portal, navigate to your resource group and select the Service Bus namespace you deployed in the previous task.

  15. In the navigation menu, in the Entities section, select Queues and then select the visits-requests queue entry.

  16. On the Overview page of the visits-requests queue, verify that the active message count is set to 1.

  17. Select Service Bus Explorer (Preview) and select Peek from start. This operation allows you to peek at the top messages on the queue, without dequeuing them.

  18. Select the message entry in the queue and review the Message Body section to confirm that its content matches the message you submitted.

You might want to inspect the code of the messaging-emulator microservice. Take a look at:

  • The dependencies for the Service Bus in the pom.xml file.
  • The PetClinicVisitRequestSender and PetClinicMessageResponsesReceiver classes in the service folder. These are the classes that enable sending and receiving of messages to and from a queue using JMS.
  • The PetClinicMessageRequest and PetClinicMessageResponse classes in the entity folder. These are the messages being sent back and forth.
  • The MessagingConfig class in the config folder. This class provides conversion to and from JSON.
  • The AzureServiceBusResource class in the web folder. This class makes use of the above classed to send a message to the Service Bus.

In the next steps you will add similar functionality to the visits service.