具有topicPattern的Spring KafkaListener和具有并发性的多个分区

时间:2020-09-30 19:00:11

标签: spring spring-boot apache-kafka

我有一个带有kafka的Spring Boot应用程序。

我想从不同的线程中接收来自kafka主题的消息(主题是动态创建的,所以我使用topicPattern =“ my-tompic-。*”),以便长时间处理来自主题my-topic-的消息-不要等待,也不要阻止处理其他主题(my-topic-uuid1,my-topic-uuid2等)的消息。

例如,我按以下顺序传递消息:

  • 00:00:00-message1-> my-topic-uuid1
  • 00:00:01-message2-> my-topic-uuid2

消息处理需要5分钟。 我想开始处理message2而不等待我的KafkaListener完成处理message1的过程。

我知道每个特定分区只能作为单独的线程,但是我不知道如何使用KafkaListener和topicPattern进行配置。

$("#route-optimize").click(async function() {
    var token = await getToken();
    console.log(token); // Returns the token
    var jobId = await getJobId(token);
    console.log(jobId); // undefined
});

async function getToken(){
    try {
        const res = await axios.post('https://www.arcgis.com/sharing/rest/oauth2/token?client_id=abc&client_secret=dfe&grant_type=client_credentials&expiration=20100');
        return res.data.access_token;
    } catch (e) {
        return 'ERROR';
    }
}

async function getJobId(token){
    var data = {
        'f': 'json',
        'token': token,
        'populate_directions': 'true',
        'uturn_policy': 'ALLOW_UTURNS',
        'depots': '{\n  "type":"features",\n  "features" : [{\n    "attributes" : {\n        "Name" : "Bay Cities Kitchens & Appliances"\n    },\n    "geometry" : {\n        "x" : -118.469630,\n        "y" : 34.037555\n    }\n  }]\n}',
        'routes': '{\n  "features": [{\n      "attributes": {\n          "Name": "Route 1",\n          "Description": "vehicle 1",\n          "StartDepotName": "Bay Cities Kitchens & Appliances",\n          "EndDepotName": "Bay Cities Kitchens & Appliances",\n          "Capacities": "4",\n          "MaxOrderCount": 3,\n          "MaxTotalTime": 60,\n        }\n      },\n      {\n    "attributes": {\n          "Name": "Route 2",\n          "Description": "vehicle 2",\n          "StartDepotName": "Bay Cities Kitchens & Appliances",\n          "EndDepotName": "Bay Cities Kitchens & Appliances",\n          "Capacities": "4",\n          "MaxOrderCount": 3,\n          "MaxTotalTime": 60,\n        }\n      }\n  ]\n}',
        'orders': '{\n  "type": "features",\n  "features": [{\n      "attributes": {\n          "Name": "Father\'s Office",\n          "ServiceTime": 10\n      },\n      "geometry": {\n          "x": -118.498406,\n          "y": 34.029445\n      }\n  },\n  {\n      "attributes": {\n          "Name": "R+D Kitchen",\n          "ServiceTime": 10\n      },\n      "geometry": {\n          "x": -118.495788,\n          "y": 34.032339\n      }\n  },\n  {\n      "attributes": {\n          "Name": "Pono Burger",\n          "ServiceTime": 10\n      },\n        "geometry": {\n        "x": -118.489469,\n        "y": 34.019000\n      }\n    },\n  {\n      "attributes": {\n          "Name": "Il Ristorante di Giorgio Baldi",\n          "ServiceTime": 10\n      },\n        "geometry": {\n        "x": -118.518787,\n        "y": 34.028508\n      }\n    },\n  {\n      "attributes": {\n          "Name": "Milo + Olive",\n          "ServiceTime": 10\n      },\n        "geometry": {\n        "x": -118.476026,\n        "y": 34.037572\n      }\n    },\n  {\n      "attributes": {\n          "Name": "Dialogue",\n          "ServiceTime": 10\n      },\n        "geometry": {\n        "x": -118.495814,\n        "y": 34.017042\n      }\n    }\n  ]\n}\n',
        'default_date': '1523664000000'
    }
    var config = {
        method: 'post',
        url: 'https://logistics.arcgis.com/arcgis/rest/services/World/VehicleRoutingProblem/GPServer/SolveVehicleRoutingProblem/submitJob',
        headers: { 
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        data : new URLSearchParams(data).toString()
    };
    axios(config).then(function (response) {
        return response.data.jobId;
    }).catch(function (error) {
        console.log(error);
    });
}

我认为只有编写自定义的使用者,在创建新主题时在单独的线程中启动它,轮询消息并最后停止该使用者,才有可能。但这似乎是一个糟糕的解决方案。

有人遇到过这种情况吗?

0 个答案:

没有答案