小编Szy*_*iak的帖子

Groovy ScriptingEngine线程安全吗?

你打电话的时候 :

 Object isThreadSafe = scriptEngine.getFactory().getParameter("THREADING");
Run Code Online (Sandbox Code Playgroud)

MULTITHREADED按以下方式返回:

但目前尚不清楚这是什么影响.

这是否意味着:

  • 我可以调用scriptEngine.eval(script,bindings); 从不同的线程和提供绑定不共享它是线程安全的?
  • 或者这是否意味着对脚本有一些限制,因此他们应该在任何共享对象上同步?.我的理解是,这似乎是正确的答案.
  • 或者是其他东西 ?

如果答案可以用一些代码说明,那将是很好的.

java groovy multithreading jsr223

7
推荐指数
1
解决办法
1392
查看次数

为什么Int类型2 ^ 31不超出GHCi的范围?

我正在阅读Haskell编程书籍并在GHCi解释器中测试提供的示例.事实证明,IntGHCi和Hugs解释器中的类型行为存在差异.根据"Haskel编程"第3章,2^31 :: Int应该超出Int类型范围.同时,在GHCi翻译中,我得到:

Prelude> 2^31 :: Int
2147483648
Run Code Online (Sandbox Code Playgroud)

而在Hugs中它的表现就像书中说的那样:

Hugs> 2^31 :: Int
-2147483648
Run Code Online (Sandbox Code Playgroud)

在GHCi中我甚至可以检查结果是否是类型 Int

Prelude> let x = 2^31 :: Int
Prelude> :type x
x :: Int
Prelude> x
2147483648
Run Code Online (Sandbox Code Playgroud)

描述差异的来源是什么?我应该从Hugs中的书中运行示例还是使用GHCi,这似乎是学习Haskell的推荐选择?我将非常感谢你的帮助.

haskell ghci hugs

6
推荐指数
1
解决办法
255
查看次数

将循环(while和for)转换为流

我已经开始使用Java 8并尝试将我的代码中的一些循环和旧语法转换为lambdas和流.

因此,例如,我正在尝试将此转换为for循环以转换为流,但我没有做到正确:

List<String> list = new ArrayList<>();
if (!oldList.isEmpty()) {// old is a List<String>
    Iterator<String> itr = oldList.iterator();
    while (itr.hasNext()) {
        String line = (String) itr.next();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (line.startsWith(entry.getKey())) {
         String newline = line.replace(entry.getKey(),entry.getValue());
                list.add(newline);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以将上面的示例转换为单个流,其中for循环内部有一个while循环.

java java-8 java-stream

6
推荐指数
2
解决办法
7283
查看次数

获取java.lang.NoSuchMethodException:使用PropertyUtils.setSimpleProperty函数时,属性“ xx”在类“类xx”中没有setter方法

我正在使用PropertyUtils.setSimpleProperty来动态调用我的setter方法,但是由于某些原因,我一直在出错。需要您的协助以找出根本原因。这是我的代码:

class FileDt {
    String reportName=null;
    String reportLocation=null;

    public String getReportName() {
        return reportName;
    }
    public void setReportName(String reportName) {
        this.reportName = reportName;
    }
    public String getReportLocation() {
        return reportLocation;
    }
    public void setReportLocation(String reportLocation) {
        this.reportLocation = reportLocation;
    }
}

class Foo {
    public static void main (String... args) {
        FileDt dt = newFileDt();
        // #1
        PropertyUtilsBean.setSimpleProperty(dt, "reportName", "abc.html");
        // #2
        PropertyUtilsBean.setSimpleProperty(dt, "reportLocation", "c://");
    }
}
Run Code Online (Sandbox Code Playgroud)

这两种方法都抛出异常

  1. 由以下原因引起:java.lang.NoSuchMethodException:属性'reportName'在org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096)的类'FileDt'中没有setter方法

  2. 由以下原因引起:java.lang.NoSuchMethodException:属性'reportLocation'在org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096)的类'FileDt'中没有setter方法

java javabeans nosuchmethoderror

5
推荐指数
1
解决办法
702
查看次数

为什么要在 @PactVerification 中断言?

我不明白断言在@PactVerification. 对我来说,这更像是一种复杂的说法1 == 1。例如:

import static org.assertj.core.api.Assertions.assertThat;
public class PactConsumerDrivenContractUnitTest {
    @Rule
    public PactProviderRuleMk2 mockProvider
      = new PactProviderRuleMk2("test_provider", "localhost", 8080, this);
    @Pact(consumer = "test_consumer")
    public RequestResponsePact createPact(PactDslWithProvider builder) {            
        return builder
          .given("test GET ")
          .uponReceiving("GET REQUEST")
          .path("/")
          .method("GET")
          .willRespondWith()
          .body("{\"condition\": true, \"name\": \"tom\"}")
    }
    @Test
    @PactVerification()
    public void givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody() {
        //when
        ResponseEntity<String> response
          = new RestTemplate().getForEntity(mockProvider.getUrl(), String.class);
        //then
        assertThat(response.getBody()).contains("condition", "true", "name", "tom");        
    }
}
Run Code Online (Sandbox Code Playgroud)

所以首先在“createPact”中我们声明

body("{\"condition\": true, \"name\": \"tom\"}")
Run Code Online (Sandbox Code Playgroud)

然后在givenGet_whenSendRequest_shouldReturn200WithProperHeaderAndBody注释中@PactVerification我们这样做

assertThat(response.getBody()).contains("condition", "true", …
Run Code Online (Sandbox Code Playgroud)

pact pact-jvm

5
推荐指数
1
解决办法
1688
查看次数

Can not infer functional interface type in groupBy Java 8

I am not sure this has been answered earlier or not. Can anyone please tell me what is the problem with third groupBy which I've written ? Why it can not infer ?

class TestGroupBy
{
    enum LifeCycle {
        ANNUAL, PERENNIAL
    }

    private String name;
    private LifeCycle lifeCycle;

    TestGroupBy(String name, LifeCycle lifeCycle) {
        this.name = name;
        this.lifeCycle = lifeCycle;
    }

    LifeCycle getLifeCycle() {
        return this.lifeCycle;
    }

    static EnumMap mySupplier() {
        return new EnumMap(TestGroupBy.class);
    }

    public static void main(String[] args) { …
Run Code Online (Sandbox Code Playgroud)

java java-8 collectors

5
推荐指数
1
解决办法
2111
查看次数

Jenkins的Groovy脚本:执行没有第三方库的HTTP请求

我需要在Jenkins中创建一个Groovy post构建脚本,我需要在不使用任何第三方库的情况下发出请求,因为这些库不能从Jenkins引用.

我试过这样的事情:

def connection = new URL( "https://query.yahooapis.com/v1/public/yql?q=" +
    URLEncoder.encode(
            "select wind from weather.forecast where woeid in " + "(select woeid from geo.places(1) where text='chicago, il')",
            'UTF-8' ) )
    .openConnection() as HttpURLConnection

// set some headers
connection.setRequestProperty( 'User-Agent', 'groovy-2.4.4' )
connection.setRequestProperty( 'Accept', 'application/json' )

// get the response code - automatically sends the request
println connection.responseCode + ": " + connection.inputStream.text
Run Code Online (Sandbox Code Playgroud)

但我还需要在POST请求中传递JSON,我不知道如何做到这一点.任何建议表示赞赏.

rest groovy http jenkins

5
推荐指数
1
解决办法
4315
查看次数

带有 Integer 键的 Groovy 映射 - DefaultGroovyMethods 中的“getAt”无法应用于 (java.lang.Integer)

我刚刚开始练习 Groovy,我有一个与地图和 IDEA IDE 相关的问题。

Integer当我尝试用作地图的密钥时,为什么 IDEA 会显示以下通知?这个简单的 Groovy 脚本运行良好并打印正确的结果。

截屏

list = [4, 7, 3, 7, 7, 1, 4, 2, 4, 2, 7, 5]

map = [:]
list.each {
    t = map[(it)]
    map[(it)] = t != null ? t + 1 : 1
}

map.each {key, value -> if (value == 1) println key}
Run Code Online (Sandbox Code Playgroud)

groovy intellij-idea

5
推荐指数
1
解决办法
1510
查看次数

Groovy 在评估没有前导零的十进制值上的关系运算符时抱怨意外标记

我在执行以下 Groovy 脚本片段时遇到问题。

GroovyShell sh = new GroovyShell();
sh.evaluate("\"abcd\".length() >= .34");
Run Code Online (Sandbox Code Playgroud)

我收到以下异常。下面提到了整个堆栈跟踪。

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: unexpected token: >= @ line 1, column 17.
    "abcd".length() >= .34d
Run Code Online (Sandbox Code Playgroud)

如果我更改.340.34,它就有效。但是,由于某些限制,我无法更改脚本内容。任何克服的帮助将不胜感激。

我遇到以下异常

Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 1: unexpected token: >= @ line 1, column 17.
    "abcd".length() >= .34d
                       ^
1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:150)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:120)
at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:132)
at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:350)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:144)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:110)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:234)
at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:168)
at …
Run Code Online (Sandbox Code Playgroud)

groovy

5
推荐指数
1
解决办法
6486
查看次数

Jenkins Pipeline sleep(10)阻止功能完成

我在Jenkinsfile中发现了Groovy代码的奇怪问题:

@NonCPS
def test() {
  println "Start"
  sleep(10)
  println "Stop"
}
Run Code Online (Sandbox Code Playgroud)

事实是,睡眠10秒钟之后,代码就再也无法进入了println "Stop"
看来,睡眠会在10秒后返回并运行下一个管道步骤。

输出只是:

[Pipieline] echo
Start
[Pipeline] sleep
Sleeping for 10 sec
[Pipeline] }
 ... next pipeline steps
Run Code Online (Sandbox Code Playgroud)

有人遇到过同样的问题吗?

groovy jenkins jenkins-pipeline

5
推荐指数
1
解决办法
3934
查看次数