Comet不会在Java多线程中启动Jetty服务器

Comet不会在Java多线程中启动Jetty服务器,java,multithreading,cometd,Java,Multithreading,Cometd,我将在Java多线程应用程序中使用CometD-3.0.2。当我创建服务频道时,一切正常。我已经在CometD initialise类中创建了套接字连接类的一个对象,它也启动了10个Java线程 public class Initializer extends GenericServlet { private MainConnect connect; @Override public void init() throws ServletException {

我将在Java多线程应用程序中使用CometD-3.0.2。当我创建服务频道时,一切正常。我已经在CometD initialise类中创建了套接字连接类的一个对象,它也启动了10个Java线程

public class Initializer extends GenericServlet
{
    private MainConnect connect;

    @Override
    public void init() throws ServletException
    {
       connect = new MainConnect();

    // Retrieve the CometD service instantiated by AnnotationCometdServlet
       StockPriceService service =   (StockPriceService)getServletContext().getAttribute(StockPriceService.class.getName());

    // Register the service as a listener of the emitter
    connect.getListeners().add(service);

    System.out.println("Service Added!");


     //Initiate the Threads
     connect.InitiateConnection();
 }
我的套接字类被启动,java线程也在运行。然而,jetty服务器从未在这里启动过

我的服务班

@Service
public class StockPriceService implements StockPriceEmitter.Listener
{
@Inject
private BayeuxServer bayeuxServer;
@Session
private LocalSession sender;

public void onUpdates(List<StockPriceEmitter.Update> updates)
{
    for (StockPriceEmitter.Update update : updates)
    {
        // Create the channel name using the stock symbol
        String channelName = "/stock/" + update.getSymbol().toLowerCase(Locale.ENGLISH);

        // Initialize the channel, making it persistent and lazy
        bayeuxServer.createIfAbsent(channelName, new ConfigurableServerChannel.Initializer()
        {
            public void configureChannel(ConfigurableServerChannel channel)
            {
                channel.setPersistent(true);
                channel.setLazy(true);
            }
        });

        // Convert the Update business object to a CometD-friendly format
        Map<String, Object> data = new HashMap<String, Object>(4);
        data.put("symbol", update.getSymbol());
        data.put("oldValue", update.getOldValue());
        data.put("newValue", update.getNewValue());

        // Publish to all subscribers
        ServerChannel channel = bayeuxServer.getChannel(channelName);
        channel.publish(sender, data, null);

        System.out.println("Service hit!!");
    }
}
}
@服务
公共类StockPriceService实现StockPriceEmitter.Listener
{
@注入
专用BayeuxServer;
@会议
专用本地会话发送器;
公共作废更新(列表更新)
{
用于(StockPriceEmitter.Update更新:更新)
{
//使用股票符号创建频道名称
字符串channelName=“/stock/”+update.getSymbol().toLowerCase(Locale.ENGLISH);
//初始化通道,使其持久化和惰性
bayeuxServer.createIfAbsent(channelName,新的ConfigurableServerChannel.Initializer()
{
公用void配置通道(ConfigurableServerChannel)
{
channel.setPersistent(true);
channel.setLazy(true);
}
});
//将更新业务对象转换为Comet友好格式
映射数据=新的HashMap(4);
data.put(“symbol”,update.getSymbol());
data.put(“oldValue”,update.getOldValue());
data.put(“newValue”,update.getNewValue());
//向所有订阅者发布
ServerChannel=bayeuxServer.getChannel(channelName);
发布(发送方,数据,空);
System.out.println(“服务命中!!”;
}
}
}
Java多线程运行方法

  if (workerID == 0) {
                    try {
                        System.out.println("Running");

                        Random random = new Random();

                        List<Update> updates = new ArrayList<Update>();

                        // Randomly choose how many stocks to update
                        int howMany = random.nextInt(symbols.size()) + 1;
                        for (int i = 0; i < howMany; ++i)
                        {
                            // Randomly choose which one to update
                            int which = random.nextInt(symbols.size());
                            String symbol = symbols.get(1);
                            float oldValue = values.get(symbol);

                            // Randomly choose how much to update
                            boolean sign = random.nextBoolean();
                            float howMuch = random.nextFloat();
                            float newValue = oldValue + (sign ? howMuch : -howMuch);

                            // Store the new value
                            values.put(symbol, newValue);

                            updates.add(new Update(symbol, oldValue, newValue));
                         //   System.out.println("Updates from Emitter Class: " + updates);
                        }

                        // Notify the listeners
                        for (Listener listener : MainConnect.getListeners())
                        {
                            System.out.println("Listners value: " + listener);
                            listener.onUpdates(updates);
                        }

                        // Randomly choose how long for the next update
                        // We use a max delay of 1 second to simulate a high rate of updates
                        long howLong = random.nextInt(1000);
                        scheduler.schedule(this, howLong, TimeUnit.MILLISECONDS);




                        Thread.sleep(3000);


                    } catch (InterruptedException  e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
if(workerID==0){
试一试{
System.out.println(“运行”);
随机=新随机();
列表更新=新建ArrayList();
//随机选择要更新的股票数量
int howMany=random.nextInt(symbols.size())+1;
对于(int i=0;i<多少;++i)
{
//随机选择要更新的
int=random.nextInt(symbols.size());
字符串符号=符号。获取(1);
float oldValue=values.get(符号);
//随机选择要更新的内容
布尔符号=random.nextBoolean();
float howmole=random.nextFloat();
浮动新值=旧值+(符号?多少:-多少);
//存储新值
value.put(符号,newValue);
添加(新更新(符号、旧值、新值));
//System.out.println(“来自发射器类的更新:“+更新”);
}
//通知听众
对于(侦听器:MainConnect.getListeners())
{
System.out.println(“Listners值:“+listener”);
onUpdate(更新);
}
//随机选择下一次更新的时间
//我们使用1秒的最大延迟来模拟高更新率
long howLong=random.nextInt(1000);
scheduler.schedule(this,howLong,TimeUnit.ms);
睡眠(3000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
总的来说,Im实现了一个服务器,它使用套接字接收客户端连接,然后从客户端接收数据。我需要这些数据流到客户端B。当我启动套接字类/打开套接字连接时,Jetty服务器不会启动


非常感谢您提供的任何信息

您必须澄清您所说的“它不会启动Jetty服务器,但我的线程开始运行”以及您所说的“java多线程”是什么意思,因为这两者对我来说都没有意义。另外,关于插座和码头的部分也没有意义。CometD是完全多线程安全的,如果您严格遵循教程说明,您的示例将运行良好。最后,您没有指定Comet版本、Jetty版本等@sbordet抱歉,我将更新我的问题again@sbordet你现在可以检查一下吗,我更新了我的问题。没有关于你的
MainConnect
类的信息,这是你问题的核心。我通过分离我的多线程和Comed解决了这个问题