HornetQ客户端STOMP

时间:2013-01-19 12:01:55

标签: hornetq stomp

为什么我不能使用 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:以下键无效   配置连接器:协议

1 个答案:

答案 0 :(得分: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();
        }
    }