Search in sources :

Example 1 with SourceTaskContext

use of io.openmessaging.connector.api.source.SourceTaskContext in project rocketmq-externals by apache.

the class RedisSourceTaskTest method initAndStartTask.

@Before
public void initAndStartTask() {
    try {
        initKeyValue();
        this.task = new RedisSourceTask();
        this.task.initialize(new SourceTaskContext() {

            @Override
            public PositionStorageReader positionStorageReader() {
                return new PositionStorageReader() {

                    @Override
                    public ByteBuffer getPosition(ByteBuffer byteBuffer) {
                        return null;
                    }

                    @Override
                    public Map<ByteBuffer, ByteBuffer> getPositions(Collection<ByteBuffer> collection) {
                        return null;
                    }
                };
            }

            @Override
            public KeyValue configs() {
                return keyValue;
            }
        });
        this.task.start(this.keyValue);
    } catch (JedisConnectionException e) {
    }
}
Also used : KeyValue(io.openmessaging.KeyValue) DefaultKeyValue(io.openmessaging.internal.DefaultKeyValue) SourceTaskContext(io.openmessaging.connector.api.source.SourceTaskContext) PositionStorageReader(io.openmessaging.connector.api.PositionStorageReader) RedisSourceTask(org.apache.rocketmq.connect.redis.connector.RedisSourceTask) ByteBuffer(java.nio.ByteBuffer) Map(java.util.Map) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Before(org.junit.Before)

Example 2 with SourceTaskContext

use of io.openmessaging.connector.api.source.SourceTaskContext in project rocketmq-externals by apache.

the class WorkerSourceTask method run.

/**
 * Start a source task, and send data entry to MQ cyclically.
 */
@Override
public void run() {
    try {
        producer.start();
        log.info("Source task producer start.");
        state.compareAndSet(WorkerTaskState.NEW, WorkerTaskState.PENDING);
        sourceTask.initialize(new SourceTaskContext() {

            @Override
            public PositionStorageReader positionStorageReader() {
                return positionStorageReader;
            }

            @Override
            public KeyValue configs() {
                return taskConfig;
            }
        });
        sourceTask.start(taskConfig);
        state.compareAndSet(WorkerTaskState.PENDING, WorkerTaskState.RUNNING);
        log.info("Source task start, config:{}", JSON.toJSONString(taskConfig));
        while (WorkerState.STARTED == workerState.get() && WorkerTaskState.RUNNING == state.get()) {
            try {
                Collection<SourceDataEntry> toSendEntries = sourceTask.poll();
                if (null != toSendEntries && toSendEntries.size() > 0) {
                    sendRecord(toSendEntries);
                }
            } catch (Exception e) {
                log.error("Source task runtime exception", e);
                state.set(WorkerTaskState.ERROR);
            }
        }
        sourceTask.stop();
        state.compareAndSet(WorkerTaskState.STOPPING, WorkerTaskState.STOPPED);
        log.info("Source task stop, config:{}", JSON.toJSONString(taskConfig));
    } catch (Exception e) {
        log.error("Run task failed.", e);
        state.set(WorkerTaskState.ERROR);
    } finally {
        if (producer != null) {
            producer.shutdown();
            log.info("Source task producer shutdown.");
        }
    }
}
Also used : ConnectKeyValue(org.apache.rocketmq.connect.runtime.common.ConnectKeyValue) KeyValue(io.openmessaging.KeyValue) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) SourceTaskContext(io.openmessaging.connector.api.source.SourceTaskContext) PositionStorageReader(io.openmessaging.connector.api.PositionStorageReader) MQClientException(org.apache.rocketmq.client.exception.MQClientException) RemotingException(org.apache.rocketmq.remoting.exception.RemotingException)

Example 3 with SourceTaskContext

use of io.openmessaging.connector.api.source.SourceTaskContext in project rocketmq-externals by apache.

the class WorkerDirectTask method startSourceTask.

private void startSourceTask() {
    state.compareAndSet(WorkerTaskState.NEW, WorkerTaskState.PENDING);
    sourceTask.initialize(new SourceTaskContext() {

        @Override
        public PositionStorageReader positionStorageReader() {
            return positionStorageReader;
        }

        @Override
        public KeyValue configs() {
            return taskConfig;
        }
    });
    sourceTask.start(taskConfig);
    state.compareAndSet(WorkerTaskState.PENDING, WorkerTaskState.RUNNING);
    log.info("Source task start, config:{}", JSON.toJSONString(taskConfig));
}
Also used : ConnectKeyValue(org.apache.rocketmq.connect.runtime.common.ConnectKeyValue) KeyValue(io.openmessaging.KeyValue) SourceTaskContext(io.openmessaging.connector.api.source.SourceTaskContext) PositionStorageReader(io.openmessaging.connector.api.PositionStorageReader)

Aggregations

KeyValue (io.openmessaging.KeyValue)3 PositionStorageReader (io.openmessaging.connector.api.PositionStorageReader)3 SourceTaskContext (io.openmessaging.connector.api.source.SourceTaskContext)3 ConnectKeyValue (org.apache.rocketmq.connect.runtime.common.ConnectKeyValue)2 SourceDataEntry (io.openmessaging.connector.api.data.SourceDataEntry)1 DefaultKeyValue (io.openmessaging.internal.DefaultKeyValue)1 ByteBuffer (java.nio.ByteBuffer)1 Map (java.util.Map)1 MQClientException (org.apache.rocketmq.client.exception.MQClientException)1 RedisSourceTask (org.apache.rocketmq.connect.redis.connector.RedisSourceTask)1 RemotingException (org.apache.rocketmq.remoting.exception.RemotingException)1 Before (org.junit.Before)1 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)1