英文:
How do I convert JMS binary stream to .NET/C# types?
问题
以下是您要翻译的内容:
我有一个发送 `MyClass` 对象实例到 JMS 的 Java 代码:
```java
MyClass mc = new MyClass("str1", 12345, "");
JmsTemplate tpl = new JmsTemplate(connectionFactory);
tpl.setReceiveTimeout(2000);
tpl.send("...", session -> {
ObjectMessage message = session.createObjectMessage();
message.setObject(mc);
message.setJMSCorrelationID("22");
return message;
});
MyClass 是一个带有属性的 Java 类:
public class MyClass implements Serializable {
String path1;
int path2;
String path3;
public MyClass(String path1, int path2, String path3)
{
this.path1 = path1;
this.path2 = path2;
this.path3 = path3;
}
}
现在我在 .NET/C# 客户端有 MyClass 实例的二进制流。如何将其反序列化为 .NET 类型,如下所示:
class MyClass {
public string Path1 { get; set; }
public int Path2 { get; set; }
public string Path3 { get; set; }
}
<details>
<summary>英文:</summary>
I have Java code that sends a `MyClass` object instance to JMS:
```java
MyClass mc = new MyClass("str1", 12345, "");
JmsTemplate tpl = new JmsTemplate(connectionFactory);
tpl.setReceiveTimeout(2000);
tpl.send("...", session -> {
ObjectMessage message = session.createObjectMessage();
message.setObject(mc);
message.setJMSCorrelationID("22");
return message;
});
MyClass is some Java class with properties:
public class MyClass implements Serializable {
String path1;
int path2;
String path3;
public MyClass(String path1, int path2, String path3)
{
this.path1 = path1;
this.path2 = path2;
this.path3 = path3;
}
}
Now I have binary stream of MyClass instance in .NET/C# client. How can I deserialize it to .NET type like:
class MyClass {
public string Path1 { get; set; }
public int Path2 { get; set; }
public string Path3 { get; set; }
}
答案1
得分: 1
将Java对象序列化后与.NET代码共享并不是您应该花时间去实现和测试的事情,因为已经有很多解决这类问题的好方法,比如使用JSON序列化对象等等protobuf等。对于JSON,您可以使用TextMessage,对于一些protobuf实现,您可以使用BytesMessage,这将是一个更快的方式,可以很容易地进行测试并且能够正常工作。
在几乎所有情况下,都应该避免使用JMS ObjectMessage,因为它可能存在潜在的安全风险,这已经迫使JMS提供商限制了可以传递给它的内容,所以从这个角度来看,我建议不要使用它。
英文:
Sharing serialized Java objects with .NET code is not something you should really spend time trying to implemented and hopefully test as there are already plenty of good solutions to such problems like using JSON serialized objects etc protobuf etc. Using a TextMessage for JSON or a BytesMessage for some protobuf impl etc is going to be a much faster route to something that works and is easily testable.
The JMS ObjectMessage should be avoided in almost every case as it poses potential security risks that have forced JMS providers to lock down what can and cannot be passed to it so just from that perspective I'd advise against its use.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论