messaging-config.xml 文件
<destination id="simple-feed"> <properties> <server> <allow-subtopics>true</allow-subtopics> <subtopic-separator>.</subtopic-separator> </server> </properties> </destination>
applicationContext.xml 文件
<flex:message-broker> <flex:message-service default-channels="my-streaming-amf,my-longpolling-amf,my-polling-amf" /> </flex:message-broker> <!-- MessageTemplate makes it easy to publish messages --> <bean id="defaultMessageTemplate" class="org.springframework.flex.messaging.MessageTemplate" /> <bean id="simpleFeedStarter" class="com.team.gis.service.SimpleFeed"> <constructor-arg ref="defaultMessageTemplate" /> <flex:remoting-destination /> <property name="stationMapper" ref="stationMapper" /> </bean>
flex 文件:
<mx:ChannelSet id="cs"> <mx:AMFChannel url="http://localhost:8080/pipe/messagebroker/amflongpolling"/> <mx:AMFChannel url="http://localhost:8080/pipe/messagebroker/amfpolling"/> </mx:ChannelSet> <mx:Consumer id="consumer" destination="simple-feed" channelSet="{cs}" message="messageHandler(event.message)"/>
as文件:
consumer.subscribe();
private function messageHandler(message:IMessage):void
{
var siteInfo:ArrayCollection = message.body as ArrayCollection;
var sites:ArrayCollection = siteInfo.getItemAt(0) as ArrayCollection;
/**清空图层数据*/
var keySets :Array = GlobeConfig.getInstance().graphicsLayerHashTable.getKeySet();
for(var k:int =0 ; k < keySets.length ; k++){
var templayer:GraphicsLayer = GlobeConfig.getInstance().graphicsLayerHashTable..find(keySets[k]);
templayer.clear();
}
GlobeConfig.getInstance().graphicHashTable.clear();
for(var a :int = 0 ; a<sites.length ; a++){
var gisLocation:GisLocation = sites.getItemAt(a) as GisLocation;
var g:Graphic = new Graphic();
g.geometry = new MapPoint(Number(gisLocation.x),Number(gisLocation.y));
g.symbol = new MonitorSymbol(gisLocation.alias,gisLocation.symbol,gisLocation.items);
g.attributes = gisLocation;
g.toolTip = gisLocation.alias;
g.addEventListener(MouseEvent.CLICK,graphicClickHandler);
var layerHashTable:HashTable = GlobeConfig.getInstance().graphicsLayerHashTable;
var layer:GraphicsLayer = layerHashTable.find(gisLocation.layerid);
if(gisLocation == null){
continue;
}
if(layer != null){
layer.add(g);
GlobeConfig.getInstance().graphicHashTable.add(gisLocation.id,g);
}
}
AppEventDispatcher.getInstance().dispatchEvent(new AppEvent(ArcgisEventConst.REFRESH_GIS_LOCATION_EVENT));
}
java代码:
package com.team.gis.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.flex.messaging.MessageTemplate;
import com.team.gis.domain.GisLocation;
import com.team.gis.domain.MonitorItem;
import com.team.gis.persistence.StationMapper;
public class SimpleFeed {
private final MessageTemplate template;
private StationMapper stationMapper;
public SimpleFeed(MessageTemplate template) {
this.template = template;
}
public void pushSiteInfo(){
List<GisLocation> locations = stationMapper.searchGisLocation();
List<MonitorItem> items = stationMapper.searchMonitorItems();
List<MonitorItem> aitems = null;
for(GisLocation gisLocation : locations){
if(gisLocation.getMonitor() != null){
String[] ms = gisLocation.getMonitor().split(",");
aitems = new ArrayList<MonitorItem>();
for(MonitorItem m : items){
for(String mi : ms){
if(m.getId() == Integer.valueOf(mi)){
m.setAvailable(true);
aitems.add(m);
}
}
}
gisLocation.setItems(aitems);
}
}
List<List<?>> siteInfo = new ArrayList<List<?>>();
siteInfo.add(locations);
siteInfo.add(items);
template.send("simple-feed",siteInfo);
}
public StationMapper getStationMapper() {
return stationMapper;
}
public void setStationMapper(StationMapper stationMapper) {
this.stationMapper = stationMapper;
}
}