Skip to main content
Skip table of contents

Working with file channels


Learning objectives
 By the end of this module, you should know:

  • How to create a file channel in NexJ Studio.

  • How to add a file channel to an environment.

  • How to create a message in script.

  • How to format and send a message to a file system in script.

Key concepts

  • File channel - A channel for reading, writing, mapping, and manipulating a file.


Creating a file channel

In this learning activity, you will create a new file channel called training:file

Learning activity

To create the file channel:

  1. In the Integration layer, select the Channels tab.
  2. Right-click the list area inside the tab and select New Channel.
  3. In the New Channel dialog, specify the following values:
    • Name: training:file
    • Type: File
  4. Click Finish. The channel opens in an editor window.

Adding the file channel to an environment

Let's make the training:file channel accessible to the Development environment by adding and configuring a channel connection.

Learning activity

To add the file channel to the Development environment:

  1. In the Deployment layer, select the Environments tab.
  2. Double-click the Development environment. The environment opens in an editor window.
  3. In the Channel Connections tab, click the Select Channel Connections button
    . The Select Channel Connections dialog opens.
  4. Add the training:file connection and click OK.
  5. In the General tab, specify the following values:
    • Temporary directory: C:\temp\training\temp
    • Outgoing directory: C:\temp\training\out
      You can use different directory names.
  6. Save the changes made to the channel.
  7. Create the following two folders in your computer's file system. If you used different names for the temporary directory and outgoing directory, create folders that match those names.
    • C:\temp\training\out
    • C:\temp\training\temp

Writing a script for a file message

In this learning activity, you will use script in a scratchpad to create a file message and send the message through the training:file channel. Your script will:

  • Create the message and specify values for all the fields.
  • Create a file name using the firstName and lastName values from the message.
  • Convert the message into file format.
  • Send the message.

Learning activity

To create the script:

  1. Enter the following code into an existing or new scratchpad.

    CODE
    ; Create the payload message
    (define msg
    	(message
    		(: :class "training:PersonXML")
    		(: firstName "Kate")
    		(: lastName "Connor")
    		(: address
    			(message
    				(: street "Broadway Ave")
    				(: city "Manhattan")
    			)
    		)
    	)
    )
    
    ; Create the output filename
    (define outFile
    	(string-append (msg'firstName) "_" (msg'lastName) ".xml")
    )
    
    
    ; Create the protocol message
    (define req
    	(message
    		(: :class "File")
    		(: file outFile)
    		(: body (format-message-pretty msg))
    	)
    )
    
    ; Send the message
    (integration-send-receive req "training:file" ())
  2. Save the scratchpad.

Using the script to test the file channel

In this learning activity, you will use the Console to run the script you created in the scratchpad.

Learning activity

To test the file channel:

  1. Run the server.
  2. In the Console, execute each of the statements from the scratchpad individually and inspect the results.
  3. View the contents of the outgoing directory created earlier. There should now be a file called Kate_Connor.xml. Open the file in a text editor and inspect the contents.
  4. In the Console, execute the last statement from the scratchpad again.

    CODE
    File already exists.
  5. In the scratchpad, remove or comment out the statement (: file outFile) and run the last two statements in the Console again. There should now be a second file in the outgoing directory.

  6. Open and review the file.

  7. In the Console, run the last statement from the scratchpad again. 

    CODE
    If you don't specify a file name, a file name is automatically created.
  8. In the Development environment General tab, inspect the value of the Outgoing Name property.

    CODE
    Sets the format of the default file name.
  9. When you are finished, stop the Console.

  10. Save the changes made to the scratchpad.
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.