Loading
Slack製品の問い合わせ先はSalesforce Helpへ統合・変更されます続きを読む
PHPの脆弱性に関する監視状況について 続きを読む

Mule SDK | How to avoid "Cannot open a new cursor on a closed stream error" when your flow returns a stream

公開日: Mar 2, 2024
解決策

SYMPTOM

When a custom java mule application is created and it has the operations which are required to return InputStream. The test cases would fail when we try to assert the response content in JUnit test cases.
Below is the example code which fails with the error "java.lang.IllegalStateException: Cannot open a new cursor on a closed stream"

@Test
public void executeRetrieveInfoOperation() throws Exception {
	Event event = flowRunner("retrieveInfoFlow").run();
	InputStream inputStream = ((CursorStreamProvider) event.getMessage().getPayload().getValue()).openCursor();	
	System.out.println(inputStream);
    String payloadValue = IOUtils.toString(inputStream);
    assertThat(payloadValue, is("Using Configuration [configId] with Connection id [aValue:100]"));
}
 

CAUSE

The stream is already closed. Normal runtime behavior is to close all streams when the flow ends.

SOLUTION

Update the code to the below one.

@Test
public void executeRetrieveInfoOperation() throws Exception {
	Event event = flowRunner("retrieveInfoFlow").keepStreamsOpen().run();
	InputStream inputStream = ((CursorStreamProvider) event.getMessage().getPayload().getValue()).openCursor();	
	System.out.println(inputStream);
    String payloadValue = IOUtils.toString(inputStream);
    assertThat(payloadValue, is("Using Configuration [configId] with Connection id [aValue:100]"));
}
The key part is the method keepStreamsOpen() which prevents the flowRunner from closing the streams.
ナレッジ記事番号

001121949

 
読み込み中
Salesforce Help | Article