Skip to content

PulsarTemplate setSchema is not thread-safe #268

Closed
@onobc

Description

@onobc

Consider the following code1:

@Autowired PulsarTemplate<Type1> template1;
@Autowired PulsarTemplate<Type2> template2;

void send1(...) {
  template1.setSchema(Schema.JSON(Type1));
  template1.send("topic-1", ...);
}

void send2(...) {
  template2.setSchema(Schema.JSON(Type2));
  template2.send("topic-2", ...);
}

Because there is only a single template instance that is assigned to both template1 and template2` it is possible that the wrong schema could be used on the wrong data type.

  • t1 send1 called and set schema to type1 and then thread gets pre-empted
  • t2 send2 called and set schema to type2 and then gets pre-empted
  • t3 send1 continues and does a send w/ a type1 object
  • 💥 type1 object does not work well w/ type2 schema

Suggested Fix

Move the setSchema to the send API params

Footnotes

  1. https://stackoverflow.com/questions/75023746/defining-multiple-pulsar-producers-in-spring-boot-application

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions