top of page

In this Blog I will share some scripts, code samples, and other technical information I have learned working in software engineering and working with WMS software.  Note these are just sample scripts and code and aren't guaranteed to work in all versions of Red Prairie / JDA / Blue Yonder WMS.

All Posts

  • thecodingguy
  • May 2
  • 2 min read

This is a Groovy script that can be used to send an XML message to a Kafka topic in a WMS env. I used it for testing sending to Kafka in WMS Dev and QA envs.


Note this is just a sample and the xml_to_send should be adjusted as you need to and the correct values should be set for the Kafka server, username, password, and topic. Also the Apache Kafka client .jar file will need to be in the WMS server environment classpath for this script to work.

It will send multiple XMLs if you use MOCA or SQL to generate multiple xml_to_send inside the { } to put into the res_send result set. There is also code commented out to perform a Kafka commit if needed and sleep between messages if needed. { publish data where xml_to_send =

"<SAMPLE>" ||

"<CTRL_SEG>" ||

"<TRNNAM>SAMPLE-TRAN</TRNNAM>" ||

"<TRNVER>2025.1</TRNVER>" ||

"<WHSE_ID>SAMPLE_WH_ID</WHSE_ID>" ||

"</CTRL_SEG>" ||

"</SAMPLE>"

} >> res_send

|

publish data where server_adr = @server_adr

and username = @username

and password = @password

and topic = @topic

and res_send = @res_send

|

[[

import org.apache.kafka.clients.producer.KafkaProducer;

import org.apache.kafka.clients.producer.ProducerConfig;

import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

import java.util.Scanner;


MocaContext ctx = MocaUtils.currentContext();

String server_address = ctx.getVariable( "server_adr" );

String username = ctx.getVariable( "username" );

String password = ctx.getVariable( "password" );

String topic_name = ctx.getVariable( "topic" );

ctx.trace( "========================================" );

ctx.trace( "Starting values - Server: " + server_address );

ctx.trace( "Username: " + username );

ctx.trace( "Password: " + password );

ctx.trace( "Topic: " + topic_name );

// Set configuration for the Producer

Properties configProperties = new Properties();


configProperties.put( ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, server_address );

configProperties.put( ProducerConfig.RETRIES_CONFIG, "0" );

configProperties.put( ProducerConfig.ACKS_CONFIG, "all" );


configProperties.put( ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer" );

configProperties.put( ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer" );


configProperties.put( "security.protocol", "SASL_SSL" );

configProperties.put( "sasl.mechanism", "PLAIN" );


String js = "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"%s\" password=\"%s\";";


configProperties.put( "sasl.jaas.config", String.format(js, username, password) );


ctx.trace( "Creating Producer Class" );

org.apache.kafka.clients.producer.Producer<String, String> producer = new KafkaProducer<>( configProperties );

try

{

res_send.reset();

//producer.beginTransaction();

while ( res_send.next() )

{

String xml_message = res_send.getString( "xml_to_send" );


ctx.trace( "Sending XML message );

ctx.trace( "Server: " + server_address );

ctx.trace( "Topic: " + topic_name );

ctx.trace( "XML is: " + xml_message );

ProducerRecord<String, String> rec = new ProducerRecord<String, String>( topic_name, xml_message );

ctx.trace( "===== Sending now =====" );

producer.send( rec );

ctx.trace( "===== Successfully sent message =====" );

// for multiple messages optional sleep - Thread.sleep( 250 );

}

//producer.commit( Transaction );

}

catch ( Exception exp )

{

ctx.trace( exp.toString() );

//producer.abortTransaction();

}

finally

{

producer.close();

ctx.trace( "After closing KafkaProducer" );

}


ctx.trace( "========================================" );

]]

Updated: May 2

This class is a utility class I wrote to connect to a JDA / BlueYonder WMS environment from Java and run MOCA commands. The constructor requires the WMS user id, password, URL, and warehouse id. Once connected, the executeMoca() methods can be used to execute MOCA commands ( with or without arguments ) and will return a MocaResults object.

Note the moca-core.jar file must be in the classpath for this class to work.


package com.thecodingguy.wms.samples;


import com.redprairie.moca.MocaArgument;

import java.util.HashMap;

import com.redprairie.moca.MocaException;

import com.redprairie.moca.MocaResults;

import com.redprairie.moca.client.ConnectionUtils;

import com.redprairie.moca.client.MocaConnection;


public class WMSConnectionManager {


    protected String wms_user;

    protected String wms_pw;

    protected String wms_url;

    protected String wms_wh_id;


    protected MocaConnection wms_connection;


    public WMSConnectionManager ( String wms_user_in, String wms_pw_in, String wms_url_in, String wms_wh_id_in ) {

        this.wms_user = wms_user_in;

        this.wms_pw = wms_pw_in;

        this.wms_url = wms_url_in;

        this.wms_wh_id = wms_wh_id_in;

    }


    public void connect() throws MocaException {

        Map<String, String> env = new HashMap<String, String>();

        env.put("WH_ID", this.wms_wh_id);

        wms_connection = ConnectionUtils.createConnection(this.wms_url, env);

        ConnectionUtils.login(wms_connection, this.wms_user, this.wms_pw);

    }


    public void close() throws MocaException {

        if (wms_connection != null) {

            wms_connection.close();

        }

    }


    public MocaResults executeMoca(String command_in) throws MocaException {

        MocaResults res = wms_connection.executeCommand(command_in );

        return res;

    }


    public MocaResults executeMoca(String command_in, MocaArgument[] args) throws MocaException {

        MocaResults res = wms_connection.executeCommandWithArgs(command_in, args);

        return res;

    }


}

Updated: May 2

This is simplified version of a Groovy script I used on JDA / Blue Yonder WMS envs to map WMS data into an object and then use the Jackson ObjectMapper class to convert it to JSON. This is a very simplified version of the script.


A SQL could be added at the beginning, such as [select * from ord] and the result set passed into the script. Then the set methods can be changed to set the ORDNUM and WH_ID from the values in the SQL results.


More fields could also be added to the Order class as well to generate a more complex JSON.

[[

public class Order

{

private String ordnum;

private String wh_id;


public String getOrdnum() {

return this.ordnum;

}


public void setOrdnum( String ordnum ) {

this.ordnum = ordnum;

}

public String getWh_id() {

return this.wh_id;

}


public void setWh_id( String wh_id ) {

this.wh_id = wh_id;

}

}

import com.fasterxml.jackson.databind.ObjectMapper;


ObjectMapper mapper = new ObjectMapper();

Order ord = new Order();

ord.setOrdnum( "TEST_ORDER" );

ord.setWh_id( "TEST_WH_ID" );


String jsonString = mapper.writeValueAsString( ord );

//String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString( ord );

output_string = jsonString;

output_string;

]]

Logo showing an abstract cartoon figure sitting in front of a laptop coding.  Next to the Logo is the text The Coding Guy

© 2025 by The Coding Guy, LLC

bottom of page