Job Processor Tutorial - Adding Jobs to the Job Processor

Perform the following steps to create and run a program that can add a job to the Distributed Computing (Job Processor) SDK. In this tutorial the Job Processor is used to convert images from any supported format to PDF using one of the LEADTOOLS Imaging SDKs. Use this tutorial in conjunction with the (Creating a Job Processor Worker) tutorial, which demonstrates how to create a Job Processor worker assembly capable of actually converting the file. For the sake of simplicity, assume the client and worker are on the same machine.

  1. Start Visual Studio

  2. Choose File->New->Project from the menu

  3. In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application" in Visual Studio 2005 or "Windows Forms Application" in Visual Studio 2008 from the Templates List

  4. Type the project name as "JobProcessorAddJobsTutorial" in the Project Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK.

  5. Drag and drop the following controls onto Form1, using the names and text values below:

    Control Type Name Text
    Button _btnAddJob Add Job
    Textbox _txtJobID
    Button _btnGetJobStatus Get Job Status
    Button _btnOpenConvertedFile Open Converted File
  6. Switch to Form1 code view (Right-click Form1 in the solution explorer then select View Code) and add the following lines at the beginning of the file after any Imports or using section if there are any:

    VB
    Imports JobProcessorAddJobsTutorial.JobService 
    Imports System.IO 
    Imports System.Xml.Serialization 
    Imports System.Diagnostics 
    C#
    using JobProcessorAddJobsTutorial.JobService; 
    using System.IO; 
    using System.Xml.Serialization; 
    using System.Diagnostics; 

  7. Add the following code to the Form1 constructor (in VB, you can copy/paste the whole Sub New code from here):

    VB
    Sub New() 
       ' This call is required by the Windows Form Designer. 
       InitializeComponent() 
       ' Add any initialization after the InitializeComponent() call. 
       ' Set the LEADTOOLS runtime license 
       ' Replace with your own key 
       Dim MY_LICENSE_FILE As String = "d:\temp\TestLic.lic" 
       Dim MY_DEVELOPER_KEY As String = "xyz123abc" 
       RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY) 
    End Sub 
    C#
    public Form1() 
    { 
       InitializeComponent(); 
       // Set the LEADTOOLS license 
       // Replace with your own key 
       string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic"; 
       string MY_DEVELOPER_KEY = "xyz123abc"; 
       RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY); 
    } 

  8. Add the following structure and method to the class (see comments for explanation).

    VB
    'This method will be used to store the conversion settings (how the file should be converted). 
    'We will serialize it at the Job Processor client, and the worker assembly will deserialize it to obtain the settings. 
    Friend Structure JobMetadata 
       Public SourceFile As String 
       Public TargetFile As String 
                           
       Public Sub New(ByVal _sourceFile As String, ByVal _targetFile As String) 
          SourceFile = _sourceFile 
          TargetFile = _targetFile 
       End Sub 
    End Structure 
                        
    'Serialize the job metadata to an xml string so it can be stored in the Job Processor database, 
    'and deserialize it later by the worker assembly when the job is processed. 
    Private Function SerializeToString(ByVal jobMetadata As JobMetadata) As String 
       Using sw As StringWriter = New StringWriter() 
          Dim xmlSerializer As XmlSerializer = New XmlSerializer(GetType(JobMetadata)) 
          xmlSerializer.Serialize(sw, jobMetadata) 
          Return sw.ToString() 
       End Using 
    End Function 
                           
    'Deserialize the JobMetadata 
    Public Function DeserializeFromString(ByVal serializedString As String) As JobMetadata 
       Dim serializedBytes As Byte() = Encoding.Unicode.GetBytes(serializedString) 
       Using ms As MemoryStream = New MemoryStream(serializedBytes) 
          Dim xmlSerializer As XmlSerializer = New XmlSerializer(GetType(JobMetadata)) 
          Return CType(xmlSerializer.Deserialize(ms), JobMetadata) 
       End Using 
    End Function 
                        
    Private Function GetJobInformation() As GetJobInformationResponse 
       Using jobServiceClient As JobServiceClient = New JobServiceClient() 
          Dim getJobInformationRequest As GetJobInformationRequest = New GetJobInformationRequest() 
          getJobInformationRequest.ID = _txtJobID.Text 
          Return jobServiceClient.GetJobInformation(getJobInformationRequest) 
       End Using 
    End Function 
    C#
    //This method will be used to store the conversion settings (how the file should be converted). 
    //We will serialize it at the Job Processor client, and the worker assembly will deserialize it to obtain the settings. 
    struct JobMetadata 
    { 
       public string SourceFile; 
       public string TargetFile; 
                        
       public JobMetadata(string _sourceFile, string _targetFile) 
       { 
          SourceFile = _sourceFile; 
          TargetFile = _targetFile; 
       } 
    } 
                        
    //Serialize the job metadata to an xml string so it can be stored in the Job Processor database, 
    //and deserialize it later by the worker assembly when the job is processed. 
    string SerializeToString(JobMetadata jobMetadata) 
    { 
       using (StringWriter sw = new StringWriter()) 
       { 
          XmlSerializer xmlSerializer = new XmlSerializer(typeof(JobMetadata)); 
          xmlSerializer.Serialize(sw, jobMetadata); 
          return sw.ToString(); 
       } 
    } 
                        
    //Deserialize the JobMetadata 
    public JobMetadata DeserializeFromString(string serializedString) 
    { 
       byte[] serializedBytes = Encoding.Unicode.GetBytes(serializedString); 
       using (MemoryStream ms = new MemoryStream(serializedBytes)) 
       { 
          XmlSerializer xmlSerializer = new XmlSerializer(typeof(JobMetadata)); 
          return (JobMetadata)xmlSerializer.Deserialize(ms); 
       } 
    } 
                        
    private GetJobInformationResponse GetJobInformation() 
    { 
       using (JobServiceClient jobServiceClient = new JobServiceClient()) 
       { 
          GetJobInformationRequest getJobInformationRequest = new GetJobInformationRequest(); 
          getJobInformationRequest.ID = _txtJobID.Text; 
          return jobServiceClient.GetJobInformation(getJobInformationRequest); 
       } 
    } 

  9. Add the following code for the _btnAddJob (Add Job) control’s Click handler:

    VB
    Private Sub _btnAddJob_Click(ByVal sender As Object, ByVal e As EventArgs) 
       Try 
          Using ofd As OpenFileDialog = New OpenFileDialog() 
             If ofd.ShowDialog() = DialogResult.OK Then 
                Using jobServiceClient As JobServiceClient = New JobServiceClient() 
                   Dim addJobRequest As AddJobRequest = New AddJobRequest() 
                   addJobRequest.UserToken = "User1" 
                                    
                   'Create the metadata to store the conversion settings. 
                   Dim convertedFilePath As string = Path.Combine(Path.GetDirectoryName(ofd.FileName), Path.GetFileNameWithoutExtension(ofd.FileName) & ".pdf") 
                   Dim jobMetadata As JobMetadata = New JobMetadata(ofd.FileName, convertedFilePath) 
                   addJobRequest.JobMetadata = SerializeToString(jobMetadata) 
                   addJobRequest.JobType = "ConvertToPDF" 
                   Dim addJobResponse As AddJobResponse = jobServiceClient.AddJob(addJobRequest) 
                   If (Not String.IsNullOrEmpty(addJobResponse.ID)) Then 
                      _txtJobID.Text = addJobResponse.ID 
                   Else 
                      MessageBox.Show("The job was not added") 
                   End If 
                End Using 
             End If 
          End Using 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    C#
    private void _btnAddJob_Click(object sender, EventArgs e) 
    { 
       try 
       { 
          using (OpenFileDialog ofd = new OpenFileDialog()) 
          { 
             if (ofd.ShowDialog() == DialogResult.OK) 
             { 
                using (JobServiceClient jobServiceClient = new JobServiceClient()) 
                { 
                   AddJobRequest addJobRequest = new AddJobRequest(); 
                   addJobRequest.UserToken = "User1"; 
                                       
                   //Create the metadata to store the conversion settings. 
                   string convertedFilePath = Path.Combine(Path.GetDirectoryName(ofd.FileName), Path.GetFileNameWithoutExtension(ofd.FileName) + ".pdf"); 
                   JobMetadata jobMetadata = new JobMetadata(ofd.FileName, convertedFilePath); 
                   addJobRequest.JobMetadata = SerializeToString(jobMetadata); 
                   addJobRequest.JobType = "ConvertToPDF"; 
                   AddJobResponse addJobResponse = jobServiceClient.AddJob(addJobRequest); 
                   if (!String.IsNullOrEmpty(addJobResponse.ID)) 
                      _txtJobID.Text = addJobResponse.ID; 
                   else 
                      MessageBox.Show("The job was not added"); 
                } 
             } 
          } 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(ex.Message); 
       } 
    } 

  10. Add the following code for the _btnGetJobStatus (Get Job Status) control’s Click handler:

    VB
    Private Sub _btnGetJobStatus_Click(ByVal sender As Object, ByVal e As EventArgs) 
       Try 
          If String.IsNullOrEmpty(_txtJobID.Text) Then 
             MessageBox.Show("No job ID has been entered") 
             Return 
          End If 
                        
          Using jobServiceClient As JobServiceClient = New JobServiceClient() 
             Dim getJobInformationResponse As GetJobInformationResponse = GetJobInformation() 
             If Not getJobInformationResponse.JobInformation Is Nothing AndAlso (Not String.IsNullOrEmpty(getJobInformationResponse.JobInformation.ID)) Then 
                'Show the job information 
                Dim jobInfo As String = String.Empty 
                jobInfo = String.Format("Job ID: {0}{1}", getJobInformationResponse.JobInformation.ID, Environment.NewLine) 
                jobInfo = jobInfo & String.Format("Add Time: {0}{1}", getJobInformationResponse.JobInformation.AddedTime, Environment.NewLine) 
                jobInfo = jobInfo & String.Format("Status: {0}{1}", getJobInformationResponse.JobInformation.Status, Environment.NewLine) 
                jobInfo = jobInfo & String.Format("Attempts: {0}{1}", getJobInformationResponse.JobInformation.Attempts, Environment.NewLine) 
                If (Not String.IsNullOrEmpty(getJobInformationResponse.JobInformation.JobType)) Then 
                   jobInfo = jobInfo & String.Format("Job Type: {0}{1}", getJobInformationResponse.JobInformation.JobType, Environment.NewLine) 
                End If 
                If Not getJobInformationResponse.JobInformation.LastStartedTime Is Nothing Then 
                   jobInfo = jobInfo & String.Format("Last Started Time: {0}{1}", getJobInformationResponse.JobInformation.LastStartedTime, Environment.NewLine) 
                End If 
                                 
                If Not getJobInformationResponse.JobInformation.LastUpdatedTime Is Nothing Then 
                   jobInfo = jobInfo & String.Format("Last Updated Time: {0}{1}", getJobInformationResponse.JobInformation.LastUpdatedTime, Environment.NewLine) 
                End If 
                If getJobInformationResponse.JobInformation.Status = JobStatus.Started Then 
                   jobInfo = jobInfo & String.Format("Progress: {0}{1}", getJobInformationResponse.JobInformation.Percentage, Environment.NewLine) 
                End If 
                If (Not String.IsNullOrEmpty(getJobInformationResponse.JobInformation.Worker)) Then 
                   jobInfo = jobInfo & String.Format("Worker: {0}{1}", getJobInformationResponse.JobInformation.Worker, Environment.NewLine) 
                End If 
                If getJobInformationResponse.JobInformation.Status = JobStatus.Completed Then 
                   jobInfo = jobInfo & String.Format("Completed Time: {0}{1}", getJobInformationResponse.JobInformation.CompletedTime, Environment.NewLine) 
                End If 
                If getJobInformationResponse.JobInformation.Status = JobStatus.Failed Then 
                   jobInfo = jobInfo & String.Format("Failed Time: {0}{1}", getJobInformationResponse.JobInformation.FailedTime, Environment.NewLine) 
                   jobInfo = jobInfo & String.Format("Error ID: {0}{1}", getJobInformationResponse.JobInformation.FailureInformation.FailedErrorID, Environment.NewLine) 
                   jobInfo = jobInfo & String.Format("Error Message: {0}{1}", getJobInformationResponse.JobInformation.FailureInformation.FailedMessage, Environment.NewLine) 
                End If 
                MessageBox.Show(jobInfo, "Job Info") 
             Else 
                MessageBox.Show("The specified job ID was not found.") 
                Return 
             End If 
          End Using 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    C#
    private void _btnGetJobStatus_Click(object sender, EventArgs e) 
    { 
       try 
       { 
          if (String.IsNullOrEmpty(_txtJobID.Text)) 
          { 
             MessageBox.Show("No job ID has been entered"); 
             return; 
          } 
                        
          using (JobServiceClient jobServiceClient = new JobServiceClient()) 
          { 
             GetJobInformationResponse getJobInformationResponse = GetJobInformation(); 
             if (getJobInformationResponse.JobInformation != null && !String.IsNullOrEmpty(getJobInformationResponse.JobInformation.ID)) 
             { 
                //Show the job information 
                string jobInfo = String.Empty; 
                jobInfo = String.Format("Job ID: {0}{1}", getJobInformationResponse.JobInformation.ID, Environment.NewLine); 
                jobInfo = jobInfo + String.Format("Add Time: {0}{1}", getJobInformationResponse.JobInformation.AddedTime, Environment.NewLine); 
                jobInfo = jobInfo + String.Format("Status: {0}{1}", getJobInformationResponse.JobInformation.Status, Environment.NewLine); 
                jobInfo = jobInfo + String.Format("Attempts: {0}{1}", getJobInformationResponse.JobInformation.Attempts, Environment.NewLine); 
                if (!String.IsNullOrEmpty(getJobInformationResponse.JobInformation.JobType)) 
                   jobInfo = jobInfo + String.Format("Job Type: {0}{1}", getJobInformationResponse.JobInformation.JobType, Environment.NewLine); 
                if (getJobInformationResponse.JobInformation.LastStartedTime != null) 
                   jobInfo = jobInfo + String.Format("Last Started Time: {0}{1}", getJobInformationResponse.JobInformation.LastStartedTime, Environment.NewLine); ; 
                if (getJobInformationResponse.JobInformation.LastUpdatedTime != null) 
                   jobInfo = jobInfo + String.Format("Last Updated Time: {0}{1}", getJobInformationResponse.JobInformation.LastUpdatedTime, Environment.NewLine); 
                if (getJobInformationResponse.JobInformation.Status == JobStatus.Started) 
                   jobInfo = jobInfo + String.Format("Progress: {0}{1}", getJobInformationResponse.JobInformation.Percentage, Environment.NewLine); 
                if (!String.IsNullOrEmpty(getJobInformationResponse.JobInformation.Worker)) 
                   jobInfo = jobInfo + String.Format("Worker: {0}{1}", getJobInformationResponse.JobInformation.Worker, Environment.NewLine); 
                if (getJobInformationResponse.JobInformation.Status == JobStatus.Completed) 
                   jobInfo = jobInfo + String.Format("Completed Time: {0}{1}", getJobInformationResponse.JobInformation.CompletedTime, Environment.NewLine); 
                if (getJobInformationResponse.JobInformation.Status == JobStatus.Failed) 
                { 
                   jobInfo = jobInfo + String.Format("Failed Time: {0}{1}", getJobInformationResponse.JobInformation.FailedTime, Environment.NewLine); 
                   jobInfo = jobInfo + String.Format("Error ID: {0}{1}", getJobInformationResponse.JobInformation.FailureInformation.FailedErrorID, Environment.NewLine); 
                   jobInfo = jobInfo + String.Format("Error Message: {0}{1}", getJobInformationResponse.JobInformation.FailureInformation.FailedMessage, Environment.NewLine); 
                } 
                MessageBox.Show(jobInfo, "Job Info"); 
             } 
             else 
             { 
                MessageBox.Show("The specified job ID was not found."); 
                return; 
             } 
          } 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(ex.Message); 
       } 
    } 

  11. Add the following code for the _btnOpenConvertedFile (Open Converted File) control’s Click handler:

    VB
    Private Sub _btnOpenConvertedFile_Click(ByVal sender As Object, ByVal e As EventArgs) 
      Try 
         If String.IsNullOrEmpty(_txtJobID.Text) Then 
            MessageBox.Show("No job ID has been entered") 
            Return 
         End If 
         Dim getJobInformationResponse As GetJobInformationResponse = GetJobInformation() 
         If Not getJobInformationResponse.JobInformation Is Nothing AndAlso (Not String.IsNullOrEmpty(getJobInformationResponse.JobInformation.ID)) Then 
            Dim jobMetadata As JobMetadata = DeserializeFromString(getJobInformationResponse.JobInformation.Metadata.JobMetadata) 
            If (Not String.IsNullOrEmpty(jobMetadata.TargetFile)) AndAlso File.Exists(jobMetadata.TargetFile) Then 
               Using process As Process = New Process() 
                  'Open converted file 
                  process.StartInfo.FileName = jobMetadata.TargetFile 
                  process.Start() 
               End Using 
            End If 
         End If 
      Catch ex As Exception 
         MessageBox.Show(ex.Message) 
      End Try 
    End Sub 
    C#
    private void _btnOpenConvertedFile_Click(object sender, EventArgs e) 
    { 
       try 
       { 
          if (String.IsNullOrEmpty(_txtJobID.Text)) 
          { 
             MessageBox.Show("No job ID has been entered"); 
             return; 
          } 
          GetJobInformationResponse getJobInformationResponse = GetJobInformation(); 
          if (getJobInformationResponse.JobInformation != null && !String.IsNullOrEmpty(getJobInformationResponse.JobInformation.ID)) 
          { 
             JobMetadata jobMetadata = DeserializeFromString(getJobInformationResponse.JobInformation.Metadata.JobMetadata); 
             if (!String.IsNullOrEmpty(jobMetadata.TargetFile) && File.Exists(jobMetadata.TargetFile)) 
             { 
                using (Process process = new Process()) 
                { 
                   //Open converted file 
                   process.StartInfo.FileName = jobMetadata.TargetFile; 
                   process.Start(); 
                } 
             } 
          } 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(ex.Message); 
       } 
    } 

  12. Build, and run the program to test it. Click the 'Add Job' button to browse to any supported image file and add it to the Job Processor database. If added successfully, the newly added job's ID will be added to the textbox. Click the 'Get Job Status' to obtain the status of your job. After the status of your job is 'Completed', click the 'Open Converted File' to open the converted file.

Note: Keep in mind the job will not actually be processed until you configure a worker to process jobs of this type (ConvertToPDF). See the Creating Job Processor Worker Assemblies tutorial for more information.

References

LEADTOOLS Job Processor Settings

Understanding The LEADTOOLS Job Processor Database

Creating Job Processor Worker Assemblies

Deploying LEADTOOLS Distributed Computing (JobProcessor) Applications

Unlocking Special LEAD Features

Help Version 20.0.2020.4.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Imaging, Medical, and Document