Search in sources :

Example 16 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.

the class AsyncReadWriteTest method writeRecords.

@Test(description = "Test which will write records to a channel using async io framework")
public void writeRecords() throws IOException, ExecutionException, InterruptedException {
    // Number of characters in this file would be 6
    ByteChannel byteChannel = TestUtil.openForWriting(currentDirectoryPath + "records.csv");
    Channel channel = new MockByteChannel(byteChannel);
    CharacterChannel characterChannel = new CharacterChannel(channel, StandardCharsets.UTF_8.name());
    DelimitedRecordChannel recordChannel = new DelimitedRecordChannel(characterChannel, "\n", ",");
    String[] recordOne = { "Foo", "Bar", "911" };
    BStringArray recordOneArr = new BStringArray(recordOne);
    DelimitedRecordWriteEvent recordWriteEvent = new DelimitedRecordWriteEvent(recordChannel, recordOneArr);
    Future<EventResult> future = eventManager.publish(recordWriteEvent);
    future.get();
    String[] recordTwo = { "Jim", "Com", "119" };
    BStringArray recordTwoArr = new BStringArray(recordTwo);
    recordWriteEvent = new DelimitedRecordWriteEvent(recordChannel, recordTwoArr);
    future = eventManager.publish(recordWriteEvent);
    future.get();
    recordChannel.close();
}
Also used : MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) DelimitedRecordWriteEvent(org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordWriteEvent) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) Channel(org.ballerinalang.nativeimpl.io.channels.base.Channel) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) ByteChannel(java.nio.channels.ByteChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) CharacterChannel(org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel) BStringArray(org.ballerinalang.model.values.BStringArray) MockByteChannel(org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel) Test(org.testng.annotations.Test)

Example 17 with EventResult

use of org.ballerinalang.nativeimpl.io.events.EventResult in project ballerina by ballerina-lang.

the class HasNextTextRecord method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(Context context, CallableUnitCallback callback) {
    BStruct channel = (BStruct) context.getRefArgument(TXT_RECORD_CHANNEL_INDEX);
    if (channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME) != null) {
        DelimitedRecordChannel textRecordChannel = (DelimitedRecordChannel) channel.getNativeData(IOConstants.TXT_RECORD_CHANNEL_NAME);
        EventContext eventContext = new EventContext(context, callback);
        HasNextDelimitedRecordEvent hasNextEvent = new HasNextDelimitedRecordEvent(textRecordChannel, eventContext);
        CompletableFuture<EventResult> event = EventManager.getInstance().publish(hasNextEvent);
        event.thenApply(HasNextTextRecord::response);
    }
}
Also used : EventContext(org.ballerinalang.nativeimpl.io.events.EventContext) BStruct(org.ballerinalang.model.values.BStruct) EventResult(org.ballerinalang.nativeimpl.io.events.EventResult) HasNextDelimitedRecordEvent(org.ballerinalang.nativeimpl.io.events.records.HasNextDelimitedRecordEvent) DelimitedRecordChannel(org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)

Aggregations

EventResult (org.ballerinalang.nativeimpl.io.events.EventResult)17 ByteChannel (java.nio.channels.ByteChannel)4 Channel (org.ballerinalang.nativeimpl.io.channels.base.Channel)4 CharacterChannel (org.ballerinalang.nativeimpl.io.channels.base.CharacterChannel)4 DelimitedRecordChannel (org.ballerinalang.nativeimpl.io.channels.base.DelimitedRecordChannel)4 EventContext (org.ballerinalang.nativeimpl.io.events.EventContext)4 MockByteChannel (org.ballerinalang.test.nativeimpl.functions.io.MockByteChannel)4 Test (org.testng.annotations.Test)4 ExecutionException (java.util.concurrent.ExecutionException)2 ReadBytesEvent (org.ballerinalang.nativeimpl.io.events.bytes.ReadBytesEvent)2 WriteBytesEvent (org.ballerinalang.nativeimpl.io.events.bytes.WriteBytesEvent)2 ReadCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.ReadCharactersEvent)2 WriteCharactersEvent (org.ballerinalang.nativeimpl.io.events.characters.WriteCharactersEvent)2 DelimitedRecordReadEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordReadEvent)2 DelimitedRecordWriteEvent (org.ballerinalang.nativeimpl.io.events.records.DelimitedRecordWriteEvent)2 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 BStringArray (org.ballerinalang.model.values.BStringArray)1