我应该如何使用来自 Websocket 消息的数据来保存 PanacheEntity?
How should I persist a PanacheEntity with data coming from a Websocket message?
当通过 Websocket 从客户端接收消息时,我试图保留以下实体:
import javax.persistence.Column;
import javax.persistence.Entity;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
@Entity
public class Penguin extends PanacheEntity{
@Column(name="penguin_name")
public String name;
}
当收到 POST 请求时,以下持久化工作:
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.transaction.Transactional;
import com.penguins.demo.pojos.Penguin;
@Path("/api")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PenguinResource {
@GET
public List<Penguin> getPenguins(){
return Penguin.listAll();
}
@POST
@Transactional
public Response addPenguin(Penguin penguin){
penguin.persist();
return Response.ok(penguin).status(201).build();
}
}
但是,以下代码在到达持久行时冻结。 message.getPenguin()
方法返回一个实际的 Penguin
引用(MessageDecoder.class
正在做它的一部分):
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import com.penguins.demo.pojos.Message;
import com.penguins.demo.pojos.Penguin;
@ServerEndpoint(value = "/waddle/{user}", decoders = MessageDecoder.class, encoders = MessageEncoder.class)
public class PenguinHub {
@OnMessage
@Transactional
public void onMessage(Session session, Message message) throws IOException {
// Handle new messages
message.setFrom(users.get(session.getId()));
// it freezes on persist :(
message.getPenguin().persist();
broadcast(message);
}
}
我是 Panache/Hibernate 的新手,如有任何帮助,我们将不胜感激。
它是这样工作的:
@Inject
ManagedExecutor managedExecutor;
@Inject
TransactionManager transactionManager;
@OnMessage
public void onMessage(Session session, Message message) throws IOException {
message.setFrom(users.get(session.getId()));
managedExecutor.submit(() -> {
try{
transactionManager.begin();
parseMessage(message); // persist the entity here
transactionManager.commit();
}catch(Exception e){
e.printStackTrace();
}
});
}
当通过 Websocket 从客户端接收消息时,我试图保留以下实体:
import javax.persistence.Column;
import javax.persistence.Entity;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
@Entity
public class Penguin extends PanacheEntity{
@Column(name="penguin_name")
public String name;
}
当收到 POST 请求时,以下持久化工作:
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.transaction.Transactional;
import com.penguins.demo.pojos.Penguin;
@Path("/api")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PenguinResource {
@GET
public List<Penguin> getPenguins(){
return Penguin.listAll();
}
@POST
@Transactional
public Response addPenguin(Penguin penguin){
penguin.persist();
return Response.ok(penguin).status(201).build();
}
}
但是,以下代码在到达持久行时冻结。 message.getPenguin()
方法返回一个实际的 Penguin
引用(MessageDecoder.class
正在做它的一部分):
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import com.penguins.demo.pojos.Message;
import com.penguins.demo.pojos.Penguin;
@ServerEndpoint(value = "/waddle/{user}", decoders = MessageDecoder.class, encoders = MessageEncoder.class)
public class PenguinHub {
@OnMessage
@Transactional
public void onMessage(Session session, Message message) throws IOException {
// Handle new messages
message.setFrom(users.get(session.getId()));
// it freezes on persist :(
message.getPenguin().persist();
broadcast(message);
}
}
我是 Panache/Hibernate 的新手,如有任何帮助,我们将不胜感激。
它是这样工作的:
@Inject
ManagedExecutor managedExecutor;
@Inject
TransactionManager transactionManager;
@OnMessage
public void onMessage(Session session, Message message) throws IOException {
message.setFrom(users.get(session.getId()));
managedExecutor.submit(() -> {
try{
transactionManager.begin();
parseMessage(message); // persist the entity here
transactionManager.commit();
}catch(Exception e){
e.printStackTrace();
}
});
}