简体   繁体   中英

Can I trigger or call a azure webjobs using java application or spring rest api

I want to trigger or call a azure webjob using my java application which is simple a spring boot application.

I have some questions like can we invoke or trigger the azure webjob using Spring Rest API call?

And if yes then how. webjob also require a username and password.

I tried something like:

@GetMapping
public String methodCall(@RequestHeader String username, @RequestHeader String password) {
    String ApiUrl = "https://myapp.scm.azurewebsites.net/api/triggeredwebjobs/webjob";
    RestTemplate resetTemplate = new RestTemplate();

    StringBuilder sb = new StringBuilder().append(ApiUrl).append(username).append(password);

    String response = resetTemplate.getForObject(sb.toString(), String.class);

    System.out.println("response:----------" + response);

    return response;
}

If you want to run a triggered azure web job, you can use the Azure REST API

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/triggeredwebjobs/{webJobName}/run?api-version=2019-08-01

Authorization: Bearer <access token>

For example

  1. Create a service principal and assign Contributor Role to the sp

  2. Code

private static String authority = "https://login.microsoftonline.com/<tenantid>/";
private static String clientId="sp clientId";
private static String secret="sp client secret";
private static  String scope="https://management.azure.com/.default";

 try {
            // use msal4j to get token
            ConfidentialClientApplication app = ConfidentialClientApplication.builder(
                    clientId,
                    ClientCredentialFactory.createFromSecret(secret))
                    .authority(authority)
                    .build();

            ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
                    Collections.singleton(scope))
                    .build();
            CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);

            HttpHeaders headers = new HttpHeaders();
            headers.set("Authorization", "Bearer " + future.get().accessToken());
            HttpEntity<String> entity = new HttpEntity<String>("",headers);
            String url ="https://management.azure.com/subscriptions/e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68/resourceGroups/0730BowmanWindow/providers/Microsoft.Web/sites/bowman0210webapp/triggeredwebjobs/test/run?api-version=2019-08-01";
            String res= restTemplate.postForObject(url,entity,String.class);
            return res;
        } catch (MalformedURLException e) {
          throw e;
        } catch (InterruptedException e) {
            throw e;
        } catch (ExecutionException e) {
            throw e;
        }finally {
            return "cannot start";
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM