package test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.BlobMessage;
import org.apache.activemq.command.ActiveMQBlobMessage;
import org.apache.activemq.command.ActiveMQQueue; public class Sending {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long a1 = System.currentTimeMillis();
/*
* First you must tell how the Blob repository can be found.
* Use ActiveMQ 5.1.0+ as broker because the fileserver webapp works there out of the box.
*/
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(
"tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:8686/fileserver/");
ActiveMQConnection conn = null;
ActiveMQSession session = null;
try {
conn = (ActiveMQConnection)cf.createConnection();
conn.start();
session = (ActiveMQSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = new ActiveMQQueue("Queue");
MessageProducer producer = session.createProducer(destination);
/*
* !!!!!!!!!!!!!!!!!!!!!!!!!
* very important. If it is set to true (default) the uploader is lost in translation ;)
* !!!!!!!!!!!!!!!!!!!!!!!!!
*/
conn.setCopyMessageOnSend(true);
File file = File.createTempFile("amq-data-file-", ".dat");
// lets write some data
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.append("Hello World!");
writer.close();
/*
* i have used an simple ascii file instead of the picture you want to use
*/
BlobMessage message = session.createBlobMessage(file);
/*
* should only work if you receive the message
*/
System.out.println(message.getInputStream());
producer.send(message);
/*
* After all you can see a new file under $Brokerlocation$/webapps/fileserver/
*/
} catch (JMSException e) {
e.printStackTrace();
} finally {
try {
if (session != null) {
session.close();
}
if (conn != null) {
conn.close();
}
} catch (JMSException ex) {
}
}
a1 = System.currentTimeMillis() - a1;
System.out.println(a1);
}
}报错:
javax.jms.JMSException: PUT was not successful: 403 Forbidden
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.BlobMessage;
import org.apache.activemq.command.ActiveMQBlobMessage;
import org.apache.activemq.command.ActiveMQQueue; public class Sending {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long a1 = System.currentTimeMillis();
/*
* First you must tell how the Blob repository can be found.
* Use ActiveMQ 5.1.0+ as broker because the fileserver webapp works there out of the box.
*/
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(
"tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:8686/fileserver/");
ActiveMQConnection conn = null;
ActiveMQSession session = null;
try {
conn = (ActiveMQConnection)cf.createConnection();
conn.start();
session = (ActiveMQSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = new ActiveMQQueue("Queue");
MessageProducer producer = session.createProducer(destination);
/*
* !!!!!!!!!!!!!!!!!!!!!!!!!
* very important. If it is set to true (default) the uploader is lost in translation ;)
* !!!!!!!!!!!!!!!!!!!!!!!!!
*/
conn.setCopyMessageOnSend(true);
File file = File.createTempFile("amq-data-file-", ".dat");
// lets write some data
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.append("Hello World!");
writer.close();
/*
* i have used an simple ascii file instead of the picture you want to use
*/
BlobMessage message = session.createBlobMessage(file);
/*
* should only work if you receive the message
*/
System.out.println(message.getInputStream());
producer.send(message);
/*
* After all you can see a new file under $Brokerlocation$/webapps/fileserver/
*/
} catch (JMSException e) {
e.printStackTrace();
} finally {
try {
if (session != null) {
session.close();
}
if (conn != null) {
conn.close();
}
} catch (JMSException ex) {
}
}
a1 = System.currentTimeMillis() - a1;
System.out.println(a1);
}
}报错:
javax.jms.JMSException: PUT was not successful: 403 Forbidden
解决方案 »
- 老师叫我们学jsp,我是新手不知从哪学起,请各位爱好学习的人士给我一个好的开始。推荐一些方法,与学习的经验,谢谢了
- java皮肤
- 如何扩展Java的JTextField使之具有JCombox的选择功能,自动查询和完成功能。就像google搜索那样输入一个关键字就自动查询与关键字相关的内容
- 如何实现窗口的隐藏
- 一个奇怪的问题啊!!想获得自己web应用所在服务器的外网ip~~查看了很多~下面的方法都不行
- 希望高手指点下一步我该怎么学
- java连接数据库的问题 能帮通俗易懂解答的牛人给50分
- 刚做好的编程技术文章系统,欢迎来翻阅(Java)技术文章.
- 下面的源程序编绎通过,但不能浏览,是不是安全性的问题?
- 请教一个关于JSWDK 的问题
- JAVA怎么学
- 我是一菜鸟,刚下了Java 2 SDK Standard Edition 1.4.2 和JCreator,第一次运行出错
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(
"tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:8686/fileserver/");这句后面那个端口8686,你看看apache-activemq-$version/conf/activemq.xml中的
<jetty ...>
<connectors>
<nioConnector port="8161"/>
</connectors>
</jetty>
nioConnector 指定的是8686吗? 不是的话你就把你程序里的改为这个端口试试.
第二:
上面端口没有问题的话,用
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("system","manager",
"tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:8686/fileserver/");
试试
不知道你的版本是什么,我的是apache-activemq-5.2.0
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- START SNIPPET: xbean -->
<beans><bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:resources/param.properties" />
</bean> <broker useJmx="true" xmlns="http://activemq.apache.org/schema/core" persistent="true" deleteAllMessagesOnStartup="false" dataDirectory="${base.data}" brokerName="localhost" useShutdownHook="false" > <persistenceAdapter>
<amqPersistenceAdapter directory="${base.data}" maxFileLength="32mb"/>
</persistenceAdapter>
<!-- In ActiveMQ 4, you can setup destination policies -->
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry queue="EUCITA.>" producerFlowControl="false" memoryLimit="10mb"/>
<policyEntry topic="EUCITA.>" producerFlowControl="false" memoryLimit="10mb">
<dispatchPolicy>
<strictOrderDispatchPolicy/>
</dispatchPolicy> <subscriptionRecoveryPolicy>
<lastImageSubscriptionRecoveryPolicy/>
</subscriptionRecoveryPolicy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<transportConnectors>
<transportConnector name="openwire" uri="tcp://localhost:61616" discoveryUri="multicast://default"/>
<transportConnector name="stomp" uri="stomp://localhost:61613"/>
<transportConnector name="xmpp" uri="xmpp://localhost:61222"/>
</transportConnectors> <networkConnectors>
<!-- by default just auto discover the other brokers -->
<networkConnector name="defaultNetwork" uri="multicast://default"/>
<!--
<networkConnector name="host1 and host2" uri="static://(tcp://host1:61616,tcp://host2:61616)" failover="true"/>
-->
</networkConnectors> </broker> <!-- lets create a command agent to respond to admin commands over JMS or XMPP on the ActiveMQ.Agent topic -->
<commandAgent xmlns="http://activemq.apache.org/schema/core"/></beans>
<!-- END SNIPPET: xbean -->