如何将JMS二进制流转换为.NET/C#类型?

huangapple go评论60阅读模式
英文:

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(&quot;str1&quot;, 12345, &quot;&quot;);

JmsTemplate tpl = new JmsTemplate(connectionFactory);
tpl.setReceiveTimeout(2000);
tpl.send(&quot;...&quot;, session -&gt; {
        ObjectMessage message = session.createObjectMessage();
        message.setObject(mc);
        message.setJMSCorrelationID(&quot;22&quot;);
        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.

huangapple
  • 本文由 发表于 2023年6月29日 16:17:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定