Search in sources :

Example 1 with SourceDataEntry

use of io.openmessaging.connector.api.data.SourceDataEntry in project rocketmq-externals by apache.

the class ActivemqSourceTask method poll.

@Override
public Collection<SourceDataEntry> poll() {
    List<SourceDataEntry> res = new ArrayList<>();
    try {
        Message message = replicator.getQueue().poll(1000, TimeUnit.MILLISECONDS);
        if (message != null) {
            Object[] payload = new Object[] { config.getDestinationType(), config.getDestinationName(), getMessageContent(message) };
            SourceDataEntry sourceDataEntry = new SourceDataEntry(sourcePartition, null, System.currentTimeMillis(), EntryType.CREATE, null, null, payload);
            res.add(sourceDataEntry);
        }
    } catch (Exception e) {
        log.error("activemq task poll error, current config:" + JSON.toJSONString(config), e);
    }
    return res;
}
Also used : SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) ArrayList(java.util.ArrayList) JMSException(javax.jms.JMSException) DataConnectException(io.openmessaging.connector.api.exception.DataConnectException)

Example 2 with SourceDataEntry

use of io.openmessaging.connector.api.data.SourceDataEntry in project rocketmq-externals by apache.

the class ActivemqSourceTaskTest method test.

// @Test
public void test() throws InterruptedException {
    KeyValue kv = new DefaultKeyValue();
    kv.put("activemqUrl", "tcp://112.74.48.251:6166");
    kv.put("destinationType", "queue");
    kv.put("destinationName", "test-queue");
    ActivemqSourceTask task = new ActivemqSourceTask();
    task.start(kv);
    for (int i = 0; i < 20; ) {
        Collection<SourceDataEntry> sourceDataEntry = task.poll();
        i = i + sourceDataEntry.size();
        System.out.println(sourceDataEntry);
    }
    Thread.sleep(20000);
}
Also used : DefaultKeyValue(io.openmessaging.internal.DefaultKeyValue) KeyValue(io.openmessaging.KeyValue) DefaultKeyValue(io.openmessaging.internal.DefaultKeyValue) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry)

Example 3 with SourceDataEntry

use of io.openmessaging.connector.api.data.SourceDataEntry in project rocketmq-externals by apache.

the class ActivemqSourceTaskTest method pollTest.

@Test
public void pollTest() throws Exception {
    ActivemqSourceTask task = new ActivemqSourceTask();
    TextMessage textMessage = new ActiveMQTextMessage();
    textMessage.setText("hello rocketmq");
    Replicator replicatorObject = Mockito.mock(Replicator.class);
    BlockingQueue<Message> queue = new LinkedBlockingQueue<>();
    Mockito.when(replicatorObject.getQueue()).thenReturn(queue);
    Field replicator = ActivemqSourceTask.class.getDeclaredField("replicator");
    replicator.setAccessible(true);
    replicator.set(task, replicatorObject);
    Field config = ActivemqSourceTask.class.getDeclaredField("config");
    config.setAccessible(true);
    config.set(task, new Config());
    queue.put(textMessage);
    Collection<SourceDataEntry> list = task.poll();
    Assert.assertEquals(list.size(), 1);
    list = task.poll();
    Assert.assertEquals(list.size(), 0);
}
Also used : Field(java.lang.reflect.Field) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) ActiveMQBytesMessage(org.apache.activemq.command.ActiveMQBytesMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) ActiveMQObjectMessage(org.apache.activemq.command.ActiveMQObjectMessage) Message(javax.jms.Message) ActiveMQMapMessage(org.apache.activemq.command.ActiveMQMapMessage) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) ActiveMQStreamMessage(org.apache.activemq.command.ActiveMQStreamMessage) Config(org.apache.rocketmq.connect.activemq.Config) Replicator(org.apache.rocketmq.connect.activemq.Replicator) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) TextMessage(javax.jms.TextMessage) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) Test(org.junit.Test)

Example 4 with SourceDataEntry

use of io.openmessaging.connector.api.data.SourceDataEntry in project rocketmq-externals by apache.

the class BaseJmsSourceTask method poll.

@Override
public Collection<SourceDataEntry> poll() {
    List<SourceDataEntry> res = new ArrayList<>();
    try {
        Message message = replicator.getQueue().poll(1000, TimeUnit.MILLISECONDS);
        if (message != null) {
            Object[] payload = new Object[] { config.getDestinationType(), config.getDestinationName(), getMessageContent(message) };
            SourceDataEntry sourceDataEntry = new SourceDataEntry(sourcePartition, null, System.currentTimeMillis(), EntryType.CREATE, null, null, payload);
            res.add(sourceDataEntry);
        }
    } catch (Exception e) {
        log.error("activemq task poll error, current config:" + JSON.toJSONString(config), e);
    }
    return res;
}
Also used : SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) StreamMessage(javax.jms.StreamMessage) BytesMessage(javax.jms.BytesMessage) ArrayList(java.util.ArrayList) JMSException(javax.jms.JMSException) DataConnectException(io.openmessaging.connector.api.exception.DataConnectException)

Example 5 with SourceDataEntry

use of io.openmessaging.connector.api.data.SourceDataEntry in project rocketmq-externals by apache.

the class MongoTest method testInitSyncCopy.

@Test
public void testInitSyncCopy() throws NoSuchFieldException, IllegalAccessException, InterruptedException {
    MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("person");
    collection.deleteMany(new Document());
    int count = 1000;
    List<String> documents = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
        Document document = new Document();
        document.put("name", "test" + i);
        document.put("age", i);
        document.put("sex", i % 2 == 0 ? "boy" : "girl");
        collection.insertOne(document);
        documents.add(document.getObjectId("_id").toHexString());
    }
    SourceTaskConfig sourceTaskConfig = new SourceTaskConfig();
    Map<String, List<String>> insterest = new HashMap<>();
    List<String> collections = new ArrayList<>();
    collections.add("*");
    insterest.put("test", collections);
    sourceTaskConfig.setInterestDbAndCollection(JSONObject.toJSONString(insterest));
    ReplicaSetConfig replicaSetConfig = new ReplicaSetConfig("", "test", "localhost");
    ReplicaSetsContext replicaSetsContext = new ReplicaSetsContext(sourceTaskConfig);
    ReplicaSet replicaSet = new ReplicaSet(replicaSetConfig, replicaSetsContext);
    Field running = ReplicaSet.class.getDeclaredField("running");
    running.setAccessible(true);
    running.set(replicaSet, new AtomicBoolean(true));
    InitSync initSync = new InitSync(replicaSetConfig, mongoClient, replicaSetsContext, replicaSet);
    initSync.start();
    int syncCount = 0;
    while (syncCount < count) {
        Collection<SourceDataEntry> sourceDataEntries = replicaSetsContext.poll();
        Assert.assertTrue(sourceDataEntries.size() > 0);
        for (SourceDataEntry sourceDataEntry : sourceDataEntries) {
            ByteBuffer sourcePartition = sourceDataEntry.getSourcePartition();
            Assert.assertEquals("test", new String(sourcePartition.array()));
            ByteBuffer sourcePosition = sourceDataEntry.getSourcePosition();
            Position position = new Position();
            position.setInitSync(true);
            position.setTimeStamp(0);
            position.setInc(0);
            Assert.assertEquals(position, JSONObject.parseObject(new String(sourcePosition.array()), Position.class));
            EntryType entryType = sourceDataEntry.getEntryType();
            Assert.assertEquals(EntryType.CREATE, entryType);
            String queueName = sourceDataEntry.getQueueName();
            Assert.assertEquals("test-person", queueName);
            Schema schema = sourceDataEntry.getSchema();
            Assert.assertTrue(schema.getFields().size() == 2);
            Object[] payload = sourceDataEntry.getPayload();
            Assert.assertTrue(payload.length == 2);
            Assert.assertEquals(payload[0].toString(), "test.person");
            Assert.assertTrue(documents.contains(JSONObject.parseObject(payload[1].toString(), Document.class).get("_id", JSONObject.class).getString("$oid")));
            syncCount++;
        }
    }
    Assert.assertTrue(syncCount == count);
}
Also used : HashMap(java.util.HashMap) Schema(io.openmessaging.connector.api.data.Schema) ArrayList(java.util.ArrayList) ConnectionString(com.mongodb.ConnectionString) Document(org.bson.Document) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) ReplicaSet(org.apache.connect.mongo.replicator.ReplicaSet) SourceDataEntry(io.openmessaging.connector.api.data.SourceDataEntry) Position(org.apache.connect.mongo.replicator.Position) ReplicaSetsContext(org.apache.connect.mongo.replicator.ReplicaSetsContext) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InitSync(org.apache.connect.mongo.initsync.InitSync) EntryType(io.openmessaging.connector.api.data.EntryType) ReplicaSetConfig(org.apache.connect.mongo.replicator.ReplicaSetConfig) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) Test(org.junit.Test)

Aggregations

SourceDataEntry (io.openmessaging.connector.api.data.SourceDataEntry)24 Schema (io.openmessaging.connector.api.data.Schema)12 DataEntryBuilder (io.openmessaging.connector.api.data.DataEntryBuilder)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 JSONObject (com.alibaba.fastjson.JSONObject)6 Field (io.openmessaging.connector.api.data.Field)5 ByteBuffer (java.nio.ByteBuffer)5 EntryType (io.openmessaging.connector.api.data.EntryType)4 Field (java.lang.reflect.Field)4 HashMap (java.util.HashMap)4 KeyValue (io.openmessaging.KeyValue)3 BytesMessage (javax.jms.BytesMessage)3 MapMessage (javax.jms.MapMessage)3 Message (javax.jms.Message)3 ObjectMessage (javax.jms.ObjectMessage)3 StreamMessage (javax.jms.StreamMessage)3 TextMessage (javax.jms.TextMessage)3 MQClientException (org.apache.rocketmq.client.exception.MQClientException)3 KVEntry (org.apache.rocketmq.connect.redis.pojo.KVEntry)3