3

I've spent the last few hours trying to find the answer in the "Apache Ofbiz Cookbook" and "Apache Ofbiz Development: The Beginner's Tutorial" how to get the DispatchContext in Ofbiz from a Java method. Ruth Hoffman's Cookbook doesn't include this code she just says "prepare any context parameters" p.43. I can understand how you get the DispatchContext when you register a service or an event but how does one do it from Java? Thanks.

I'm trying to populate Ofbiz entities/tables that I've defined in Ofbiz from the server end of a REST web service in Tomcat.

    GenericDelegator delegator = ctx.getDelegator();
    GenericValue myTable = delegator.makeValue("MyTable");
    myTable.set("name", myTableAsJson.getString("name"));


    try {
        delegator.store(offering);
    } catch(Exception e) {

    }
4

2 回答 2

5

In Java method/event, dispatach context can be fetched using the following code snippet:

GenericDispatcher dispatcher = (GenericDispatcher) request.getAttribute("dispatcher");
DispatchContext dctx =  dispatcher.getDispatchContext();

In Java method/event, any service can be invoked using the following code snippet:

GenericDispatcher dispatcher = (GenericDispatcher) request.getAttribute("dispatcher");
dispatcher.runSync("SERVICE_NAME", context);

where context is map of required IN/IN-OUT parameters to service.

To invoke service from Java method/event, there is no need to paas dispatch context. Only the service name and context is required.

于 2012-11-12T18:24:30.087 回答
0

This method doesn't work anymore. The HttpServletRequest coming from the controller will still have an attribute "dispatcher" but you have to cast it to a LocalDispatcher class now and then use that to get the DispatchContext.

This is from the OFBiz service engine documentation[1]: "Internal Usage of the Services Framework is quite simple. In a Web Application the LocalDispatcher is stored in the ServletContext which can be accessed via the Session object in an Event."

This worked for me:

Delegator delegator = (Delegator) request.getAttribute("delegator");
LocalDispatcher ld = (LocalDispatcher) request.getAttribute("dispatcher");
DispatchContext dctx = ld.getDispatchContext();

[1] https://cwiki.apache.org/confluence/display/OFBIZ/Service+Engine+Guide

于 2020-09-24T19:17:41.063 回答