Hot questions for Using Azure in azure webjobs

Top Java Programmings / Azure / azure webjobs

Question:

Can we write Azure Webjobs with Queue Triggered jobs on Java, tried to search a lot and found only .Net related code example (https://azure.microsoft.com/en-in/documentation/articles/websites-dotnet-webjobs-sdk-storage-queues-how-to/)

If yes can any one direct me to the resources.

Note: Was able to run webjob in java which was triggered by schedule, for this had to create executable jar and create a batch file which runs the jar and zip this two files (bat and jar).


Answer:

While we don't have plans to add Java support directly to the core WebJobs SDK, we do have a tracking item here for Java Support in Azure Functions. Feel free to chime in on that issue.

All the work we're doing for cross language is being done in Azure Functions, which builds on the WebJobs SDK foundation. Azure Functions currently supports QueueTrigger (and all the other triggers) for C#, Node.js, PowerShell, PHP, Python, BAT, Bash.

Question:

History for context: I am trying to run a web job from an HTTP Client. The file is a ZIP file . and contains a java class and bat file to run that java class. This runs okay when i do from POSTMAN. But when i use HTTP client, i get the following error always " '---i-NPsGbTVUpaP0CeJxMQVrHoDHvaxo3' is not recognized as an internal or external command" - Please help – Jagaran yesterday

@Jagaran if it only happen from some clients, it is likely unrelated. Please ask a new question – David Ebbo 21 hours ago

No any HTTP Client i am using in java, it is the same. it works in CURL or loading from web console. My sample code below – Jagaran 2 hours ago

No any HTTP Client i am using in java, it is the same. it works in CURL or loading from web console.

Do you have any sample Java based HTTP Client where I can publish Azure Web Job? I have tried all Java REST clients.

May be i am doing something wrong. The error I get in Azure console is '---i-NPsGbTVUpaP0CeJxMQVrHoDHvaxo3' is not recognized as an internal or external command, [08/25/2017 09:30:22 > e7f683: ERR ] operable program or batch file.o

I feel Content type = applciation /zip is not happening correctly when using java. Please help us. Sample Code:

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.http.entity.ContentType;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;


/**
 * @author jagaran.das
 *
 */
public class AIPHTTPClient {

    /**
     * @param args
     * @throws IOException 
     */
    @SuppressWarnings({ "unused", "rawtypes" })
    public static void main(String[] args) throws IOException {
        try {

            URI  uri = new AIPHTTPClient().getURI();
            HttpResponse<InputStream> jsonResponse = Unirest.put("https://<URL>/api/triggeredwebjobs/TestJOb")
                .basicAuth("$AzureWebJobTestBRMS", "XXXXX")
                    .header("content-disposition","attachement; filename=acvbgth.bat")
                    .field("file", new  FileInputStream(new File(uri)) 
                        ,ContentType.create("content-type: application/zip"),"AzureWebJob.zip").asBinary();
             System.out.println(jsonResponse.getStatusText()); 


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public InputStream readZip() {
        ZipFile zipFile = null;
        ZipEntry zipEntry = zipFile.getEntry("run.bat");

        InputStream stream = null;
    /*  try {
            zipFile = new ZipFile("/Users/jagaran.das/Documents/work/AIP/AzureWebJob.zip");
            java.util.Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while(entries.hasMoreElements()){
                ZipEntry entry = entries.nextElement();
                stream = zipFile.getInputStream(entry);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } */
        try {
            stream = zipFile.getInputStream(zipEntry);
        } catch (IOException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
        }
   return stream;

    }


  public URI getURI() throws MalformedURLException {
    File file = new File("/Users/jagaran.das/Documents/work/AIP/azure-poc/AzureWebJob.zip");

    URI fileUri = file.toURI();
    System.out.println("URI:" + fileUri);


    URL fileUrl = file.toURI().toURL();
    System.out.println("URL:" + fileUrl);

    URL fileUrlWithoutSpecialCharacterHandling = file.toURL();
    System.out.println("URL (no special character handling):" + fileUrlWithoutSpecialCharacterHandling);
    return fileUri;
       }

}

Answer:

I've been a little too harsh in my answer before really trying stuff out. Apologies. I've now tried out your snippet and looks like you're hitting an issue with Unirest - probably this one.

My advice would be to just move to Apache's HTTP library. Here's a working sample:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.File;

public class App 
{
    public static void main( String[] args )
    {
        File sourceZipFile = new File("webjob.zip");
        String kuduApiUrl = "https://yoursitename.scm.azurewebsites.net/api/zip/site/wwwroot/app_data/jobs/triggered/job988/";

        HttpEntity httpEntity = EntityBuilder.create()
        .setFile(sourceZipFile)
        .build();

        CredentialsProvider provider = new BasicCredentialsProvider();
        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(
            "$yoursitename", "SiteLevelPasSw0rD"
        );
        provider.setCredentials(AuthScope.ANY, credentials);

        HttpClient client = HttpClientBuilder.create()
            .setDefaultCredentialsProvider(provider)
            .build();

        HttpPut putRequest = new HttpPut(kuduApiUrl);
        putRequest.setEntity(httpEntity);
        // Kudu's Zip API expects application/zip
        putRequest.setHeader("Content-type", "application/zip");

        try {
            HttpResponse response = client.execute(putRequest);
            int statusCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            String resBody = EntityUtils.toString(entity, "UTF-8");
            System.out.println(statusCode);
            System.out.println(resBody);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

That's sending Content-Type: application/zip and the raw zip contents in the body (no multipart horse manure). I've probably over-engineered the sample.. but it is what it is.

The upload is successful and the WebJob published:

Question:

I need to install and call a Powershell script from an Azure Web job. The Powershell script will then call a java based command line tool.

And also, in an Azure environment, how would I know what the path to the files (Powershell and Java) would even be?

I am thinking of something like this:

private static void CallPowershell([QueueTrigger("queueName")CloudQueueMessage, TextWriter log)
{
  Process.Start("/pathToFile.ps");
}

Thanks.


Answer:

Running a PowerShell script in WebJobs is native, but here is a post on how to get it done.

http://blogs.msdn.com/b/nicktrog/archive/2014/01/22/running-powershell-web-jobs-on-azure-websites.aspx

The challenge will be how to make that PowerShell run the Java code.

Question:

I am currently working with a Java console application on a Windows server that has mounted an Azure File Share. I can't use the SDK of Azure Storage on that case because I use a framework this can only treat local files(and I can not change that.). At this point everything is working perfectly.

The client wants now to move the application to an Azure Function. The problem is that I don't see any way to mount the Azure File Share in an Azure Function. I've also tried Azure Web App but as I've seen so far there are not many options for Java.

What would be the best way to do that?

Thanks in advance for your help.


Answer:

According to my research, we cannot mount Azure file share in Azure Function or Azure web app. Because Azure app service cannot connect to anywhere using ports 445, 137, 138, and 139. However, if you want to mount Azure File share, you need to open port 445. For more details, please refer to the Accessing Azure File Storage from Azure Function and the document.

So if you want to manage Azure File share in Azure Function, you need to Azure Storage SDK to manage it.

Besides, if you just want to mount Azure File share in Azure service, I suggest you use the Azure VM. Once you mount Azure file share on Azure VM, you can manage it like local file system.