锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
One approach for doing this to create a new UserType as follows.
package mypackage; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import java.sql.Blob; import cirrus.hibernate.Hibernate; import cirrus.hibernate.HibernateException; import cirrus.hibernate.UserType; public class BinaryBlobType implements UserType { public int[] sqlTypes() { return new int[] { Types.BLOB }; } public Class returnedClass() { return byte[].class; } public boolean equals(Object x, Object y) { return (x == y) || (x != null && y != null && java.util.Arrays.equals((byte[]) x, (byte[]) y)); } public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { Blob blob = rs.getBlob(names[0]); return blob.getBytes(1, (int) blob.length()); } public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { st.setBlob(index, Hibernate.createBlob((byte[]) value)); } public Object deepCopy(Object value) { if (value == null) return null; byte[] bytes = (byte[]) value; byte[] result = new byte[bytes.length]; System.arraycopy(bytes, 0, result, 0, bytes.length); return result; } public boolean isMutable() { return true; } }
The BinaryBlobType will convert a blob into a byte array and back again.
Here's how to use it. First, define an entity that contains a byte[] property:
public class ImageValue { private long id; private image byte[]; public long getId() { return id; } public void setId(long id) { this.id = id; } public byte[] getImage() { return image; } public void setImage(byte[] image) { this.image = image; } }
Then map a blob column onto the byte[] property:
<class name="ImageValue" table="IMAGE_VALUE">
<id name="id/>
<property name="image" column="IMAGE" type="mypackage.BinaryBlobType"/>
</class>
Notes:
1) Blobs aren't cachable. By converting the blob into a byte array, you can now cache the entity.
2) This approach reads the whole blob into memory at once.
3) The above type is known to work for reading blobs out of the db. Other usage patterns might also work.
Comments (GK)
I changed isMutable() to return true, since an array is a mutable object.
The use of setBlob() will work on some drivers, but not all. I think its more portable to use setBytes() or even setBinaryStream().
comments (db)
db's comment above was right, setBlob() didn't work on Oracle, I used setBytes().
comments (Chad Woolley)
Below is a modified nullsafeset() that i needed to use to get it to work with tomcat 4.1.27 & oracle 8/9i - the normal calls don't work through the tomcat/dbcp connection pool wrapper objects... (this caused me great pain)
pls note that the setBytes() doesn't seem to work with oracle driver & hibernate
d.birch@eclipsegroup.com.au
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { if(st instanceof org.apache.commons.dbcp.DelegatingPreparedStatement && ((org.apache.commons.dbcp.DelegatingPreparedStatement)st).getDelegate() instanceof oracle.jdbc.OraclePreparedStatement) { oracle.sql.BLOB blob = oracle.sql.BLOB.createTemporary( ((org.apache.commons.dbcp.PoolableConnection)st.getConnection()).getDelegate(), false, oracle.sql.BLOB.DURATION_SESSION); blob.open(BLOB.MODE_READWRITE); OutputStream out = blob.getBinaryOutputStream(); try { out.write((byte[])value); out.flush(); out.close(); } catch(IOException e) { throw new SQLException("failed write to blob" + e.getMessage()); } blob.close(); ((oracle.jdbc.OraclePreparedStatement)((org.apache.commons.dbcp.DelegatingPreparedStatement)st).getDelegate()).setBLOB(index, blob); } else { st.setBlob(index, Hibernate.createBlob((byte[]) value)); } } //and.. note the null check, oracle drivers return a null blob... public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { final Blob blob = rs.getBlob(names[0]); return blob != null?blob.getBytes(1, (int)blob.length()):null; }
/ comments Vanitha
I had to use the user type to save pdfs as oraBLOBs in oracle 91 database. nullsafeSet
needed a sligh modification , or else ther was a classcastexception. Used oracle Blob instead of Hibernate Blob type and it works.
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { oracle.sql.BLOB t_blob = oracle.sql.BLOB.createTemporary(((org.jboss.resource.adapter.jdbc.WrappedConnection) st.getConnection()).getUnderlyingConnection(), false, oracle.sql.BLOB.DURATION_SESSION); OutputStream t_out = null; t_blob.open(BLOB.MODE_READWRITE); t_out = t_blob.getBinaryOutputStream(); try { t_out.write((byte[]) value); t_out.flush(); t_out.close(); } catch (IOException e) { throw new SQLException("failed write to blob" + e.getMessage()); } t_blob.close(); st.setBlob(index, t_blob); }
</code>
1. 鍒涘緩琛細
2. 鍒涘緩瀛樺偍榪囩▼錛?
榪欎釜results_out鏄竴涓父鏍囩被鍨嬶紝鐢ㄦ潵榪斿洖鏌ユ壘鐨勭粨鏋滈泦銆?/p>
3.銆瀹屾暣瀹炵幇浠g爜錛?
4.銆嫻嬭瘯浠g爜錛屽湪榪欓噷浣跨敤浜咼unit4嫻嬭瘯錛?
5. 嫻嬭瘯鐨勮緭鍑虹粨鏋滐細
ActiveMQ 瀹炶返涔嬭礬(鍥? ActiveMQ 4.x +JBoss 4.x MDP瀹炴垬綃?/font> xml 浠g爜
鍏朵腑destination,destinationType鏄痳a.xml閲岄潰鎻愪緵鐨勯厤緗睘鎬?(榪欓噷瀹樻柟鐨勬枃妗f槸Destination,DestinationType, 鑰屽疄闄呬笂activemq-ra.rar閲岄潰鐨剅a.xml鏄痙estination,destinationType錛屾敞鎰忓ぇ灝忓啓鍖哄埆) 浜?font face="Arial">. 閰嶇疆jboss.xml xml 浠g爜
2.) 浣跨敤ActiveMQ RA鐨勯厤緗?/p> xml 浠g爜
2. jboss.xml 閰嶇疆invoker proxy鍜宑ontainer 鏀寔ActiveMQ RA xml 浠g爜
MessageDrivenBean鐨刢ontainer閰嶇疆,榪欓噷鐨?container-name>蹇呴』鍜屼笂闈㈢殑
xml 浠g爜
浠ヤ笂灝辨槸ActiveMQ+JBoss InBound 鐨勯厤緗?/font> 涓?鍦⊿ervlet涓氳繃鍙戦佹秷鎭?楠岃瘉涓婇潰鐨凪essage Driven Bean 涓轟簡楠岃瘉榪欎釜MessageDrivenBean鑳藉姝e父宸ヤ綔,鎴戜嬌鐢ㄤ竴涓緢綆鍗曠殑servlet鍚戣繖涓猶ueue鍙戦佹秷鎭?鍓嶄竴綃囩殑activemq-ds.xml 宸茬粡鎻愪緵鍦ㄥ惎鍔ㄧ殑鏃跺欑粦瀹氫簡JNDI activemq/QueueConnectionFactory,activemq/queue/outbound,鎴戜滑鐩存帴浣跨敤灝辮浜? java 浠g爜
鍥?鍏充簬durable鏂瑰紡璁㈤槄topic鐨勮ˉ鍏呰鏄?br /> 浣跨敤durable鏂瑰紡,浣犻渶瑕佸湪ejb-jar.xml涓澶栫殑閰嶇疆,subscriptionDurability,clientId,subscriptionName xml 浠g爜
ok 榪欐牱灝卞彲浠ヤ嬌鐢╠urable鏂瑰紡璁㈤槄topic浜嗐?/p> 鍙傝冩枃妗? |
ActiveMQ鏈韓鏄紑婧愰」鐩?鎵浠ラ噰鐢ˋctiveMQ鐨勯」鐩線寰涔熸槸浠ュ叾浠栧紑婧愯蔣浠跺叡鍚屾瀯寤?鐩墠涓繪祦寮婧愬簲鐢ㄦ湇鍔″櫒鏈塀oss,geronimo,JOnAs錛岃屽叾涓璯eronimo 榛樿鐨凧MS Provider灝辨槸ActiveMQ錛岄偅鎴戜滑灝辯潃閲嶄粙緇岮ctiveMQ涓嶫Boss,JOnAs鐨勬暣鍚堟柟妗?/p> 鏈枃鍙傝冧簡 Integrating Apache ActiveMQ with JBoss涓?a >JBoss Integration,鍐嶆牴鎹瑪鑰呭疄闄呮暣鍚堢粡楠屾葷粨鑰屾垚銆?/p> 涓.鏁村悎闇瑕佺殑鐜. 浜?鏁村悎姝ラ 1. 姝ラ涓: 瑙e帇activemq-rar-4.1.0-incubator.rar 鍒?jboss-4.0.5.GA\server\default\deploy\activemq-ra.rar (榪欎釜鏄洰褰曞悕瀛? 涓嬮潰鏄痑ctivemq-rar.rar鐩綍涓嬮潰鐨勬枃浠跺拰瀛愮洰褰?璇鋒敞鎰忕孩鑹叉爣璁扮殑鍦版柟(鍚庨潰浼氶愪竴璇存槑,鏁村悎鐨勮繃紼? activeio-core-3.0.0-incubator.jar activemq-core-4.1.0-incubator.jar activemq-ra-4.1.0-incubator.jar backport-util-concurrent-2.1.jar commons-logging-1.0.3.jar derby-10.1.1.0.jar geronimo-j2ee-management_1.0_spec-1.0.jar spring-2.0.jar spring-1.2.6.jar xbean-spring-2.7.jar broker-config.xml META-INF 2.姝ラ浜? 鍒犻櫎澶氫綑鐨?font color="#ff0000">spring-1.2.6.jar,鐢變簬4.1.0鐨剅a閲岄潰鍖呭惈浜?涓笉鍚岀増鏈殑spring浼氳Е鍙戜竴涓猠xception鐨勪駭鐢?https://issues.apache.org/activemq/browse/AMQ-1124, 鑰屼笖涓轟簡浠ュ悗鑳藉浣跨敤鏂扮殑spring schema閰嶇疆鏂瑰紡,鎴戜滑榪欓噷浼氬垹闄?font color="#ff0000">spring-1.2.6.jar,淇濈暀spring-2.0.jar銆?鏈鏂扮殑snapshot version鐨剅a宸茬粡鍘繪帀浜嗚繖涓浣欑殑spring-1.2.6.jar). 3.姝ラ涓? 淇敼META-INF\ra.xml,璁㎎Boss浣跨敤broker-config.xml 浣滀負榛樿鐨勯厤緗枃浠墮厤緗産orker. 淇敼涓嬮潰鐨勫湴鏂?br />
銆銆銆銆 鏀逛負:
銆銆銆銆琛ㄧず浣跨敤broker-config.xml鏉ラ厤緗惎鍔ˋctiveMQ. 4.姝ラ鍥? 淇敼borker-config.xml,榛樿鐨刡orker-config.xml浼氫駭鐢熶竴涓敊璇?鏃犺鏄垜浣跨敤鐨勭増鏈繕鏄渶鍚庣殑snapshot鐗堟湰,榛樿鐨刡orker-config.xml閮戒細璁﹛bean-spring 2.7(snapshot 浣跨敤鐨勬槸2.8)鎶涘嚭exception.瑙e喅鐨勫姙娉曞涓?/div>
灝?nbsp;
鏀逛負
鍗沖彲
5.姝ラ浜? 灝唜bean-spring-2.7.jar (鎴栬呮槸2.8) 澶嶅埗鍒癹boss-4.0.5.GA\server\default\lib涓嬮潰
涓?浣跨敤鏁村悎瀹屾瘯鐨凙ctiveMQ浣滀負ds緇戝畾鍒癑Boss鐨凧NDI鏈嶅姟銆?/div>
緙栧啓jboss-4.0.5.GA\server\default\depoly\activemq-ds.xml
xml 浠g爜
鍚姩JBoss.濡傛灉鐪嬭浠ヤ笅淇℃伅灝辮〃紺篈ctiveMQ宸茬粡鎴愬姛鍚姩,騫朵笖浣跨敤涓婇潰鐨刣s閰嶇疆鏂囦歡鎴愬姛鍦板皢topic/queue緇戝畾鍒頒簡JNDI鏈嶅姟涓娿?/div>
......
[TransportConnector] Connector tcp://localhost:61616 Started
[NetworkConnector] Network Connector bridge Started [BrokerService] ActiveMQ JMS Message Broker (localhost, ID:MyNoteBook-2165-1173250880171-1:0) started ......
[ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=ConnectionFactoryBinding,name=activemq/QueueConnectionFactory' to JNDI name 'java:activemq/QueueConnectionFactory'
[ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=ConnectionFactoryBinding,name=activemq/TopicConnectionFactory' to JNDI name 'java:activemq/TopicConnectionFactory' [AdminObject] Bound admin object 'org.apache.activemq.command.ActiveMQQueue' at 'activemq/queue/outbound' [AdminObject] Bound admin object 'org.apache.activemq.command.ActiveMQTopic' at 'activemq/topic/inbound ......
鍥?楠岃瘉ActiveMQ+JBoss
榪欓噷浣犲彲浠ヤ嬌鐢ㄧ畝鍗曠殑jms client榪炴帴鍒癰roker-config.xml閲岄潰鐨勫崗璁繛鎺ュ櫒涓婇潰,榛樿鐨勬槸tcp://localhost:61616
鍦ㄥ悗闈㈡垜浠細鍦ㄦ鏁村悎鍩虹涓婂紑鍙慚essage Driver Bean鍜屼嬌鐢╯pring MDP 2縐嶆瀯鏋?nbsp;鏉ラ獙璇佹湰嬈ctiveMQ+JBoss鐨勬暣鍚堛?/div>
|
鏈瘒涓昏璁茶В鍦ㄦ湭浣跨敤鍏朵粬妗嗘灦(Spring)鏁村悎鎯呭喌涓?鐙珛鍩轟簬ActiveMQ,浣跨敤JMS瑙勮寖榪涜娑堟伅閫氫俊銆?br />
涓.JMS鍥為【
鍥犱負ActiveMQ鏄竴涓狫MS Provider鐨勫疄鐜?鍥犳鍦ㄥ紑濮嬪疄浣滃墠,鏈夊繀瑕佸涔犱笅JMS鐨勫熀紜鐭ヨ瘑
Java Message Service (JMS)鏄痵un鎻愬嚭鏉ョ殑涓篔2EE鎻愪緵浼佷笟娑堟伅澶勭悊鐨勪竴濂楄鑼?JMS鐩墠鏈?濂楄鑼冭繕鍦ㄤ嬌鐢↗MS 1.0.2b鍜?.1. 1.1宸茬粡鎴愪負涓繪祦鐨凧MS Provider浜嬪疄涓婄殑鏍囧噯浜?
*1.1涓昏鍦╯ession涓婇潰鏈変竴浜涢噸瑕佹敼鍙?姣斿鏀寔寤虹珛鍚屼竴session涓婄殑transaction,璁╀粬鏀寔鍚屾椂鍙戦丳2P(Queue)娑堟伅鍜屾帴鍙?br />
Topic娑堟伅銆?br />
鍦↗MS涓棿涓昏瀹氫箟浜?縐嶆秷鎭ā寮廝oint-to-Point (鐐瑰鐐?,Publich/Subscribe Model (鍙戝竷/璁㈤槄鑰?錛?br />
鍏朵腑鍦≒ublich/Subscribe 妯″紡涓嬪張鏈塏ondurable subscription鍜宒urable subscription (鎸佷箙鍖栬闃?2縐嶆秷鎭鐞嗘柟寮忋?br />
涓嬮潰鏄疛MS瑙勮寖鍩烘湰鐨勬帴鍙e拰瀹炵幇
JMS Common Interfacse PTP-Specific Interface Pub/Sub-specific interfaces
ConnectionFactory QueueConnectionFactory TopicConnectionFactory
Connection QueueConnection TopicConnection
Destination Queue Topic
Session QueueSession TopiSession
MessageProducer QueueSender TopicPublisher
MessageConsumer QueueReceiver/QueueBrwer TopicSubscriber
浜?浣跨敤Queue
涓嬮潰浠ctiveMQ example鐨勪唬鐮佷負涓昏繘琛岃鏄?br />
1. 浣跨敤ActiveMQ鐨凜onnection錛孋onnectionFactory 寤虹珛榪炴帴,娉ㄦ剰榪欓噷娌℃湁鐢ㄥ埌pool
//寤虹珛Connection
//寤虹珛Session
2銆傚彂閫佹秷鎭殑浠g爜
//寤虹珛QueueSession
//浣跨敤Producer鍙戦佹秷鎭埌Queue
3銆傛帴鍙楁秷鎭?鍦↗MS瑙勮寖閲岄潰,浣犲彲浠ヤ嬌鐢?br />
浠ヤ笂灝辨槸浣跨敤jms queue鍙戦佹帴鍙楁秷鎭殑鍩烘湰鏂瑰紡
涓?Topic
1. 寤虹珛榪炴帴
2. 寤虹珛Session
3.鍒涘緩Producer 鍙戦佹秷鎭埌Topic
4.鍒涘緩Consumer鎺ュ彈娑堟伅(鍩烘湰涓婂拰Queue鐩稿悓)
鍥?榪炴帴ActiveMQ鐨勬柟寮?br />
ActiveMQConnectionFactory 鎻愪緵浜嗗縐嶈繛鎺ュ埌Broker鐨勬柟寮?a title="ActiveMQ榪炴帴鍗忚" >activemq.apache.org/uri-protocols.html
甯歌鐨勬湁
vm://host:port //vm
tcp://host:port //tcp
ssl://host:port //SSL
stomp://host:port //stomp鍗忚鍙互璺ㄨ璦,鐩墠鏈夊緢澶氱stomp client 搴?java,c#,c/c++,ruby,python...);
涓.瀹夎ActiveMQ
棣栧厛鍘籬ttp://activemq.apache.org/download.html 涓嬭澆鏈鏂扮増鏈?.1.0release (http://activemq.apache.org/activemq-410-release.html),
瑙e帇apache-activemq-4.1-incubator.zip(鎴栬卆pache-activemq-4.1-incubator.tar.gz)鐩綍濡備笅:
+bin (windows涓嬮潰鐨刡at鍜寀nix/linux涓嬮潰鐨剆h)
+conf (activeMQ閰嶇疆鐩綍,鍖呭惈鏈鍩烘湰鐨刟ctiveMQ閰嶇疆鏂囦歡)
+data (榛樿鏄┖鐨?
+docs (index,replease鐗堟湰閲岄潰娌℃湁鏂囨。,-.-b涓嶇煡閬撲負鍟ヤ笉甯?
+example (鍑犱釜渚嬪瓙
+lib (activemMQ浣跨敤鍒扮殑lib)
-apache-activemq-4.1-incubator.jar (ActiveMQ鐨刡inary)
-LICENSE.txt
-NOTICE.txt
-README.txt
-user-guide.html
浣犲彲浠ヤ嬌鐢╞in\activemq.bat(activemq) 鍚姩,濡傛灉涓鍒囬『鍒?浣犲氨浼氱湅瑙佺被浼間笅闈㈢殑淇℃伅
(緇嗚妭鍙兘涓嶄竴鏍?姣斿璺緞,鎴栬卝mx,jdbc淇℃伅)
ACTIVEMQ_HOME: D:\java\framework_and_lib\activemq\apache-activemq-4.1-incubator\
bin\..
Loading message broker from: xbean:activemq.xml
INFO BrokerService - ActiveMQ null JMS Message Broker (localho
st) is starting
INFO BrokerService - For help or more information please see:
http://incubator.apache.org/activemq/
INFO ManagementContext - JMX consoles can connect to service:jmx:r
mi:///jndi/rmi://localhost:1099/jmxrmi
INFO JDBCPersistenceAdapter - Database driver recognized: [apache_derby
_embedded_jdbc_driver]
INFO DefaultDatabaseLocker - Attempting to acquire the exclusive lock
to become the Master broker
INFO DefaultDatabaseLocker - Becoming the master on dataSource: org.ap
ache.derby.jdbc.EmbeddedDataSource@1d840cd
INFO JournalPersistenceAdapter - Journal Recovery Started from: Active Jou
rnal: using 5 x 20.0 Megs at: D:\java\framework_and_lib\activemq\apache-activemq
-4.1-incubator\activemq-data\journal
INFO JournalPersistenceAdapter - Journal Recovered: 0 message(s) in transa
ctions recovered.
INFO TransportServerThreadSupport - Listening for connections at: tcp://P-SUW
EI:61616
WARN MulticastDiscoveryAgent - brokerName not set
INFO TransportConnector - Connector default Started
INFO TransportServerThreadSupport - Listening for connections at: stomp://P-S
UWEI:61613
INFO TransportConnector - Connector stomp Started
INFO NetworkConnector - Network Connector default Started
INFO BrokerService - ActiveMQ JMS Message Broker (localhost, I
D:P-SUWEI-1207-1170916242296-1:0) started
*銆傚嚑涓皬鎻愮ず
1. 榪欎釜浠呬粎鏄渶鍩虹鐨凙ctiveMQ鐨勯厤緗?寰堝鍦版柟閮芥病鏈夐厤緗洜姝や笉瑕佺洿鎺ヤ嬌鐢ㄨ繖涓厤緗敤浜庣敓浜х郴緇?br />
2. 鏈夌殑鏃跺欑敱浜庣鍙h鍗犵敤,瀵艱嚧ActiveMQ閿欒,ActiveMQ鍙兘闇瑕佷互涓嬬鍙?099(JMX),61616(榛樿鐨凾ransportConnector)
3. 濡傛灉娌℃湁鐗╃悊緗戝崱,鎴栬匨S鐨凩oopBackAdpater Multicast浼氭姤涓涓敊璇?/p>
浜? 嫻嬭瘯浣犵殑ActiveMQ
鐢變簬ActiveMQ鏄竴涓嫭绔嬬殑jms provider,鎵浠ユ垜浠笉闇瑕佸叾浠栦換浣曠涓夋柟鏈嶅姟鍣ㄥ氨鍙互椹笂鍋氭垜浠殑嫻嬭瘯浜?緙栬瘧
example鐩綍涓嬮潰鐨勭▼搴?br />
ProducerTool/ConsumerTool 鏄疛MS鍙傝冮噷闈㈡彁鍒扮殑鍏稿瀷搴旂敤,Producer浜х敓娑堟伅,Consumer娑堣垂娑堟伅
鑰屼笖榪欎釜渚嬪瓙榪樺彲浠ュ姞鍏ュ弬鏁板府鍔╀綘嫻嬭瘯鍒氭墠鍚姩鐨勬湰鍦癆ctiveMQ鎴栬呮槸榪滅▼鐨凙ctiveMQ
ProducerTool [url] broker鐨勫湴鍧,榛樿鐨勬槸tcp://localhost:61616
[true|flase] 鏄惁浣跨敤topic,榛樿鏄痜alse
[subject] subject鐨勫悕瀛?榛樿鏄疶OOL.DEFAULT
[durabl] 鏄惁鎸佷箙鍖栨秷鎭?榛樿鏄痜alse
[messagecount] 鍙戦佹秷鎭暟閲?榛樿鏄?0
[messagesize] 娑堟伅闀垮害,榛樿鏄?55
[clientID] durable涓簍rue鐨勬椂鍊?闇瑕侀厤緗甤lientID
[timeToLive] 娑堟伅瀛樻椿鏃墮棿
[sleepTime] 鍙戦佹秷鎭腑闂寸殑浼戠湢鏃墮棿
[transacte] 鏄惁閲囩敤浜嬪姟
ConsumerTool [url] broker鐨勫湴鍧,榛樿鐨勬槸tcp://localhost:61616
[true|flase] 鏄惁浣跨敤topic,榛樿鏄痜alse
[subject] subject鐨勫悕瀛?榛樿鏄疶OOL.DEFAULT
[durabl] 鏄惁鎸佷箙鍖栨秷鎭?榛樿鏄痜alse
[maxiumMessages] 鎺ュ彈鏈澶ф秷鎭暟閲?0琛ㄧず涓嶉檺鍒?br />
[clientID] durable涓簍rue鐨勬椂鍊?闇瑕侀厤緗甤lientID
[transacte] 鏄惁閲囩敤浜嬪姟
[sleepTime] 鎺ュ彈娑堟伅涓棿鐨勪紤鐪犳椂闂?榛樿鏄?,onMeesage鏂規硶涓嶄紤鐪?br />
[receiveTimeOut] 鎺ュ彈瓚呮椂
鎴戜滑榪欐牱鍙互浣跨敤:
java -classpath .\apache-activemq-4.1-incubator.jar;example\bin ProducerTool tcp://192.168.3.142:61616 test.mysubject
java -classpath .\apache-activemq-4.1-incubator.jar;example\bin ConsumerTool tcp://192.168.3.142:61616 test.mysubject
褰撶劧浣犲彲浠ヤ嬌鐢ㄤ笂闈㈢殑鍙傛暟榪涜鏇村鏉傜殑嫻嬭瘯,鎸佷箙,浜嬪姟
濡傛灉鍑虹幇涓嬮潰鐨勪俊鎭?鎭枩浣?浣犵殑ActiveMQ宸茬粡鑳藉宸ヤ綔浜?br />
Connecting to URL: tcp://192.168.3.142:61616
Publishing a Message with size 255 to queue: TOOL.DEFAULT
Using non-durable publishing
Sleeping between publish 0 ms
Sending message: Message: 0 sent at: Thu Feb 08 15:05:34 CST 2007 ...
Sending message: Message: 1 sent at: Thu Feb 08 15:05:34 CST 2007 ...
銆傘傘傘傘傘傘傘?/p>
Connecting to URL: tcp://192.168.3.142:61616
Consuming queue: test.mysubject
Using non-durable subscription
Received: Message: 0 sent at: Thu Feb 08 14:51:34 CST 2007 ...
Received: Message: 1 sent at: Thu Feb 08 14:51:34 CST 2007 ...
銆傘傘傘?/p>
涓?灝忕粨
鎴戜滑宸茬粡涓嬭澆,鍚姩,騫朵笖鐢ㄧ▼搴忔祴璇曚簡鎴戜滑鐨凙ctiveMQ,鑰屽悗闈㈠皢鍦ㄨ繖涓兘璺戝緱ActiveMQ榪涗竴姝ョ殑璧頒笅鍘?涓姝ヤ竴姝ュ睍紺篈ctiveMQ鐨勯珮綰х壒鎬с?/p>
鍏堢湅web.xml閲岄潰鍏充簬activemq鐨刬nvoke
<!--璋冪敤activemq -->
<context-param >
<param-name>brokerURI </param-name >
<param-value>/WEB-INF/activemq.xml </param-value >
</context-param>
<listener>
<listener-class>org.activemq.web.SpringBrokerContextListener</listener-class>
</listener>
閮侀椃浜嗕笁澶╋紝浠婂ぉ緇堜簬鎶奐MS寮勫嚭鏉ヤ簡錛屽氨鏄彂閫佹秷鎭紝鐒跺悗娑堟伅鐩戝惉鍣ㄦ帴鏀跺埌浜嗘秷鎭悗鍙戦侀偖浠剁粰綆$悊鍛?/p>
鍏堢湅web.xml閲岄潰鍏充簬activemq鐨刬nvoke
<!--璋冪敤activemq -->
<context-param >
<param-name>brokerURI </param-name >
<param-value>/WEB-INF/activemq.xml </param-value >
</context-param>
<listener>
<listener-class>org.activemq.web.SpringBrokerContextListener</listener-class>
</listener>
鐒跺悗鏄湪涓婁笅鏂囦腑瀹氫箟鐨凧msTemplate鍜宎ctivemq鐩戝惉
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--JMS Template-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<bean class="org.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
</property>
<property name="defaultDestinationName" value="Hello.Queue"/>
</bean>
<bean id="activeMQContainer" class="org.activemq.jca.JCAContainer">
<property name="workManager">
<bean id="workManager" class="org.activemq.work.SpringWorkManager"/>
</property>
<property name="resourceAdapter">
<bean id="activeMQResourceAdapter"
class="org.activemq.ra.ActiveMQResourceAdapter">
<property name="serverUrl" value="tcp://localhost:61616"/>
</bean>
</property>
</bean>
<!--鐩戝惉 Message 鐨凪essage Driven POJO-->
<bean id="HelloPlaceBean" class="com.officetao.jms.HelloMDP" autowire="byName"/>
<bean id="HelloMDP" factory-method="addConnector" factory-bean="activeMQContainer">
<property name="activationSpec">
<bean class="org.activemq.ra.ActiveMQActivationSpec">
<property name="destination" value="Hello.Queue"/>
<property name="destinationType" value="javax.jms.Queue"/>
</bean>
</property>
<property name="ref" value="HelloBean" />
</bean>
</beans>
寤虹珛涓涓ā鎷熺殑鍙戦佹秷鎭殑bean錛屽唴瀹瑰涓?/font>
final String mailContent = "鏂板鍗曞彿涓?000鐨勮鍗? 閲戦";
try {
if (jmsTemplate != null)
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session)
throws JMSException {
Message message = session.createMessage();
message.setStringProperty("content", mailContent);
return message;
}
});
}
catch (Exception e) {
logger.error("JMS error when place order:", e);
}
鏈鍚庡氨鏄洃鍚秷鎭劧鍚庨噰鍙栬鍔ㄧ殑bean
public class HelloMDP implements MessageListener {
public void onMessage(javax.jms.Message arg0) {
try {
subAuthenticator subauth = new subAuthenticator("閭鐧婚檰鍚?,"瀵嗙爜");//smtp楠岃瘉 authenticator
props.put("mail.smtp.host","smtp.163.com");
props.put("mail.smtp.auth","true");
session = Session.getInstance(props,subauth);
MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO,new InternetAddress("鎺ユ敹閭歡鐨勯偖綆?/font>"));
message.setFrom(new InternetAddress("鑷繁鐨勯偖綆?/font>"));
message.setSubject("ok");
message.setText("if you see it,it works!");
Transport.send(message);
}
catch(AuthenticationFailedException e1){
System.out.println("SMTP璁よ瘉鍑洪敊錛?);
}
catch (MessagingException e) {
e.printStackTrace();
}
}
public static Properties props = System.getProperties();
public static Session session = null;
/**
*姝ゅ唴閮ㄧ被瀹氫箟浜唖mtp璁よ瘉鏂規硶
*/
public class subAuthenticator extends Authenticator{
private String userName;
private String password;
public subAuthenticator(String user,String pass){
userName=user;
password=pass;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(userName,password);
}
|
|