简体   繁体   English

在 Mule 4 中调用 groovy 方法

[英]Invoking groovy method in Mule 4

I have built a complete groovy project with JPA repo for data persistence operations.我已经使用 JPA 存储库构建了一个完整的 groovy 项目,用于数据持久化操作。 Now I want to invoke the methods from the script in Mule 4 without changing anything in the groovy project.现在我想从 Mule 4 中的脚本调用方法,而不更改 groovy 项目中的任何内容。

eg.例如。 CustomerService.groovy (pseudo code) CustomerService.groovy(伪代码)

import com.example.dao.CustomerDao.groovy
... other imports...

@Service
class CustomerService {
@Autowired
CustomerDao cDao

@Transactional
publid Customer createCustomer(Customer customer) {
    return cDao.save(customer)
}

... other methods...
}

CustomerDao.groovy CustomerDao.groovy

import spring.JPA  (the original import path may vary)

@Repository
class CustomerDao implements JPARepository<Customer, Integer> {
}

This project is working in Mule 3. In Mule 3 we have an Invoke component which could be used to invoke the methods from the groovy script.该项目在 Mule 3 中运行。在 Mule 3 中,我们有一个 Invoke 组件,可用于调用 groovy 脚本中的方法。 The Mule 4 the Invoke component is compatible only with Java and not groovy. Mule 4 Invoke 组件仅与 Java 兼容,而不与 groovy 兼容。

The Scripting module's ' Execute ' component can invoke a groovy script but not sure how to invoke the method.脚本模块的“执行”组件可以调用 groovy 脚本,但不确定如何调用该方法。 Is the any work around for this in Mule 4? Mule 4中是否有任何解决方法?

Problems occured as of now目前出现的问题

  1. In ' Execute ' component if I import another file I get the error as在“执行”组件中,如果我导入另一个文件,则会收到错误消息
Unalbe to resolve class com.example.dao.CustomerDao
@ line 3 column 1,
import com.example.dao.CustomerDao.groovy
^

Found a solution for similar problem https://help.mulesoft.com/s/article/Compilation-exception-in-Mule4-x-when-using-Groovy-script-with-Import-statement but unable to implement it.找到了类似问题https://help.mulesoft.com/s/article/Compilation-exception-in-Mule4-x-when-using-Groovy-script-with-Import-statement但无法实现的解决方案。

In the article, he developer had an issue with an apache dependency, which he/she could get from mvn repo.在文章中,他的开发人员遇到了 apache 依赖项的问题,他/她可以从 mvn repo 获得。 I am trying to import a groovy file which I have developed.我正在尝试导入我开发的 groovy 文件。 So unable to add it in the dependency.因此无法将其添加到依赖项中。 I tried adding the groovy project in my local repo and fetch it but it didn't work.我尝试在我的本地仓库中添加 groovy 项目并获取它,但它没有用。 Moreover, when this Mule 4 application will be deployed on CloudHub it will have an issue as it won't be able to access my local repo.此外,当这个 Mule 4 应用程序部署在 CloudHub 上时,它会出现问题,因为它无法访问我的本地存储库。

Need a solution to add a spring-groovy project to Mule 4需要一个解决方案来向 Mule 4 添加一个 spring-groovy 项目

Thanks:)谢谢:)

The problem is that the Groovy script executed by the Scripting Module is trying to import a class, which is not in Java's classpath.问题是脚本模块执行的 Groovy 脚本试图导入不在 Java 的类路径中的 class。 So it throws the error.所以它会抛出错误。 The class is in another Groovy source file. class 位于另一个 Groovy 源文件中。 What you can do is to compile the Groovy code into an actual Java class.您可以做的是将 Groovy 代码编译成实际的 Java class。 You can try one the methods described in the Groovy documentation to integrate a Maven plugin with your Mule application pom.xml file.您可以尝试Groovy 文档中描述的方法之一,将 Maven 插件与您的 Mule 应用程序 pom.xml 文件集成

Another alternative if you have a large number of Groovy classes is to create a separate project to build a Jar library, then use the solution from the KB article you mentioned.如果您有大量 Groovy 类,另一种选择是创建一个单独的项目来构建 Jar 库,然后使用您提到的知识库文章中的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM