DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • A Spring Boot App With Half the Startup Time
  • Key Takeaways From Integrating a RAG Application With LangSmith
  • Improving Java Application Reliability with Dynatrace AI Engine

Trending

  • Securing Branch Networks With Firewalls, VPNs, IDS/IPS, and Identity-Based Access
  • Exploring A Few Java 25 Language Enhancements
  • Add Observability to Your React Native Application in 5 Minutes
  • Building Reliable Async Processing Pipelines Using Temporal

Scheduled Restart on CloudHub

Have you ever been required to restart applications in CloudHub at a specific time of the day on weekdays? Automate it with CloudHub API and make life easier.

By 
Vince Soliza user avatar
Vince Soliza
·
Feb. 26, 16 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.8K Views

Join the DZone community and get the full member experience.

Join For Free

Have you ever been required to restart applications in CloudHub at a specific time of the day on weekdays? For example, in a testing environment when the app should be restarted at the beginning of the day before the testers commence their duties. This might sound simple as it's just few clicks to restart an application in CloudHub.

But...

What if you have a different shift and you're unable to do it on time?

What if one day you forgot to do it?

What if there are too many applications to restart?

The solution is to make this thing automated, but how? No problem, we can do it by utilizing CloudHub API and a time-based job scheduler.

What do we need?

  • Access to CloudHub API service: https://anypoint.mulesoft.com/cloudhub/api/applications/{domain}/status

  • X-ANYPNT-ENV-ID: The environment where the application deployed example: 6ebdb30c-b6bf-4d75-b8ba-61cb87b5ed4b

  • Domain - The application domain example: cloudhub-restarter

  • Anypoint Platform for API account with 'Manage Settings' role.

  • A Time-based job scheduler example: CRON, Mulesoft Poll Scheduler

After we got all of the things we need, let's create the application in Mule with a scheduled restart at 6:00 AM every weekdays.

This simple flow used Poll Scheduler to configure a polling schedule using cron expressions. When the given schedule is attained it will invoke the CloudHub REST API to restart the application using the defined values in the properties.

Image title

Put the environment id, username, and password in mule-app.properties. Replace {domain} with the application domain you want to be restarted.

Image title

Here's the Mule configuration XML.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:schedulers="http://www.mulesoft.org/schema/mule/schedulers"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/schedulers http://www.mulesoft.org/schema/mule/schedulers/current/mule-schedulers.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:request-config name="HTTP_Request_Configuration"
protocol="HTTPS" host="${cloudhub.restapi.host}" port="${cloudhub.restapi.port}"
basePath="${cloudhub.restapi.base.path}" doc:name="HTTP Request Configuration">
<http:basic-authentication username="${anypoint.platform.account.username}"
password="${anypoint.platform.account.password}" />
</http:request-config>
<flow name="cloudhub-restarterFlow">
<poll doc:name="Poll">
<schedulers:cron-scheduler expression="${cloudhub.restart.schedule}" />
<flow-ref name="invoke-restart-Flow" doc:name="invoke-restart-Flow" />
</poll>
<logger message="The Application has been restarted." level="INFO"
doc:name="Logger" />
</flow>
<flow name="invoke-restart-Flow">
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{
"status":"restart"
}]]></dw:set-payload>
</dw:transform-message>
<http:request config-ref="HTTP_Request_Configuration"
path="${cloudhub.restapi.application.status.endpoint}" method="POST"
doc:name="HTTP">
<http:request-builder>
<http:header headerName="X-ANYPNT-ENV-ID"
value="${anypoint.platform.environment.id}" />
</http:request-builder>
</http:request>
</flow>
</mule>

See how simple, fast, and easy CloudHub RESTful API can be used, if we explore more there is more than just restarting that we can do with the help of this API. See the full API reference.

Please leave a comment :)

application

Opinions expressed by DZone contributors are their own.

Related

  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector
  • A Spring Boot App With Half the Startup Time
  • Key Takeaways From Integrating a RAG Application With LangSmith
  • Improving Java Application Reliability with Dynatrace AI Engine

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook