0

为什么我不能使用hornetq-core-client.2.2.21.Final.jar创建一个简单的 stomp 客户端?

Map<String, Object> properties = new HashMap<String, Object>();
properties.put("host", "localhost");
properties.put("port", 61612);
properties.put("protocol", "stomp");
TransportConfiguration transportConfiguration = new TransportConfiguration(NettyConnectorFactory.class.getName(), properties);
ServerLocator serverLocator = HornetQClient.createServerLocatorWithoutHA(transportConfiguration);
ClientSessionFactory clientSessionFactory = serverLocator.createSessionFactory();
ClientSession clientSession = clientSessionFactory.createSession();
clientSession.createQueue("queue", "queue", true);
ClientProducer clientProducer = clientSession.createProducer("queue");
ClientMessage clientMessage = clientSession.createMessage(true);
clientMessage.getBodyBuffer().writeString("Hello");
clientProducer.send(clientMessage);

我收到以下错误:

java.lang.IllegalStateException:以下键对配置连接器无效:协议

4

1 回答 1

0

您应该使用 Stomp 协议来发送消息:

public void sendViaStomp(Serializable obj, String queueName) 
    {
        try 
        {
            Socket socket = new Socket("127.0.0.1",61612);

            String connectFrame = "CONNECT\n" +
                    "login: guest\n" +
                    "passcode: guest\n" + 
                    "request-id: 1\n" +
                    "\n"+
                    END_OF_FRAME;
            sendFrame(socket, connectFrame);

            String text = (String) obj;
            String messageFrame = "SEND\n" +
                    "destination: jms.queue." + queueName + "\n" +
                    "\n"+
                    text + 
                    END_OF_FRAME;
            sendFrame(socket, messageFrame);

            System.out.println("Sent Stomp Message:" + text);

            String disconnectFrame = "DISCONNECT\n" +
                    "\n" + 
                    END_OF_FRAME;
            sendFrame(socket, disconnectFrame);

            socket.close();
        } 
        catch (UnknownHostException e) 
        {
            e.printStackTrace();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
private void sendFrame(Socket socket, String data)
    {
        byte[] bytes;
        try 
        {
            bytes = data.getBytes("UTF-8");
            OutputStream outputStream = socket.getOutputStream();
            for (int i = 0; i < bytes.length; i++)
            {
                outputStream.write(bytes[i]);
            }
            outputStream.flush();
        } 
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        } 
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
于 2013-02-09T08:53:47.100 回答